In WebApi I can decorate a parameter on a controller action with [FromUri]
to have the components of the URI 'deserialized', if you will, into a POCO model; aka model-binding.
Despite using MVC since 2.0, I've never used it for websites (I don't know why). What's the equivalent of it in ASP.NET MVC 5?
The attribute doesn't seem to be recognised in the IDE unless I need to reference a library.
I'd like ~/thing/2014/9
to bind to the model below:
public class WhateverModel
{
public int Year { get; set; }
public int Month { get; set; }
}
Thanks
Update
In the other question (link above), the OP says:
However, switch this to plain MVC not WebApi and the default model binder breaks down and cannot bind the properties on objects in the nested array
Which implies that he's using the attribute from the WebApi. I guess. I don't have those references, because I'm in MVC, so is (ab)using WebApi's version the accepted way to do this in MVC?
Update 2
In the answer to that question is:
You need to construct your query string respecting MVC model binder naming conventions.
Additionally
[FromUri]
attribute in your example action is completely ignored, since it's not known to MVC DefaultModelBinder
So I'm still left not knowing what to do or what on Earth the OP was even talking about in that question, if he was getting some success with the wrong attribute.
I guess I'm hoping for a clear answer and not the mud of that other question.
Using [FromUri] To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter. The following example defines a GeoPoint type, along with a controller method that gets the GeoPoint from the URI.
When the ASP.NET Web API calls a method on a controller, it must set values for the parameters, a process called parameter binding. In order to bind a model (an action parameter), that would normally default to a formatter, from the URI we need to decorate it with [FromUri] attribute.
ASP.NET MVC 6 comes with some new features as well. Some prominent ones are: - MVC, WEB API and Web Pages are merged into one single framework.
It'll Just Work™:
[HttpGet]
public ActionResult Thing(WhateverModel model)
{
// use model
return View();
}
At least, when using the URL /thing?Year=2014&Month=9
.
The problem is your routing. The URL /thing/2014/9
won't map using MVC's default route, as that is /{controller}/{action}/{id}
, where {id}
is an optional int
.
The easiest would be to use attribute routing:
[HttpGet]
[Route("/thing/{Year}/{Month}"]
public ActionResult Thing(WhateverModel model)
{
// use model
return View();
}
This will map the URL to your model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With