I've created a custom MVC Model Binder which gets called for every HttpPost
that comes into the server. But does not get called for HttpGet
requests.
GET
? If so, what did I miss?QueryString
from a GET
Request?Here's my implementation...
public class CustomModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// This only gets called for POST requests. But I need this code for GET requests.
}
}
Global.asax
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
//...
}
I've looked into these solutions, but they don't quite work for what I'm looking for:
TempData
?Name=John&Surname=Doe
)Thanks to @Felipe for the help. Just in case someone struggles with the same, I learnt:
GET
requestsDefaultModelBinder
classGET
Requests, which makes sense when you think about it)To register custom model binder, we need to create a binder provider. The model binder provider class implement IModelBinderProvider interface. The all built-in model binders have their own model binder providers. We can also specify the type of argument model binder produces, not the input of our model binder.
In the MVC pattern, Model binding maps the HTTP request data to the parameters of a Controllers action method. The parameter can be of a simple type like integers, strings, double etc. or they may be complex types. MVC then binds the request data to the action parameter by using the parameter name.
What Is Model Binding? ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods.
IModelBinder interface - This defines methods that are required for a Model Binder, like the BindModel method. This method is responsible for binding a model to some values using ControllerContext and BindingContext.
Let's supose you have your own type you want to bind.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
// other properties you need
}
You can create a custom model bind for this specific type, inherithing from DefaultModelBinder
, for sample:
public class PersonModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
int id = Convert.ToInt32(request.QueryString["id"]);
string name = request.QueryString["name"];
int age = Convert.ToInt32(request.QueryString["age"]);
// other properties
return new Person { Id = id, Name = name, Age = age };
}
}
In the Global.asax in the Application_Start
event, you can registry this model bind, for sample:
// for Person type, bind with the PersonModelBinder
ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());
In the BindModel
method from the PersonModelBinder
, make sure you have all parameters in the querystring and give them the ideal treatment.
Since you have this action method:
public ActionResult Test(Person person)
{
// process...
}
You can access this action with an url something like this:
Test?id=7&name=Niels&age=25
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