Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC Model Binding Complex Object using GET

I have a class in my web project:

public class MyClass
{
    public int? Param1 { get; set; }
    public int? Param2 { get; set; }
}

which is a parameter in my controller method:

public ActionResult TheControllerMethod(MyClass myParam)
{
    //etc.
}

If I call the method using POST, the model binding works automatically (I use angular on the js side, which likely doesn't matter):

$http({
    method: "post",
    url: controllerRoot + "TheControllerMethod",
    data: {   
        myParam: myParam
    }
}).success(function (data) {
    callback(data);
}).error(function () {
    alert("Error getting my stuff.");
});

If I use a GET, the parameter is always null in the controller.

$http({
    method: "get",
    url: controllerRoot + "TheControllerMethod",
    params: {   
        myParam: myParam
    }
}).success(function (data) {
    callback(data);
}).error(function () {
    alert("Error getting my stuff.");
});

Does complex model binding using the default model binder only work for POSTs, or is there something I can do to make this work with a GET?

like image 319
Phil Sandler Avatar asked Jan 07 '14 20:01

Phil Sandler


People also ask

How to bind a property in MVC Model?

MVC model binding has several attributes like [BindRequired] or [BindNever] to specify binding is a must or binding can be ignored for the property. You can also customize the data source of property to read values from HTTP Header, Query string, Form fields, or Route. The model binder must bind the value of a property.

What is ASP NET Model binding?

Model Binding is a process of ASP.NET Core framework to Extract Data from HTTP Requests and provide them to the arguments of Action Method. Let us see the first example of Model Binding process. So run your project and go to URL – /Home/Index/1.

How do you bind a complex class to a parameter?

When model binding occurs, the class is instantiated using the public default constructor. For each property of the complex type, model binding looks through the sources for the name pattern prefix.property_name. If nothing is found, it looks for just property_name without the prefix. For binding to a parameter, the prefix is the parameter name.

What is model binder in MVC?

It is the process of creating .NET objects using the data sent by the browser in an HTTP request. The ASP.NET Web Forms developers who are new to ASP.NET MVC are mostly confused about how the values from View get converted to the Model class when it reaches the Action method of the Controller class, so this conversion is done by the Model Binder.


2 Answers

The answer is Yes. The difference between GET and POST requests is that a POST body can have a content type so they can be interpreted correctly on the server side as XML, or Json, so on; for GET, all you have is just a querystring.

like image 81
Lin Avatar answered Oct 19 '22 16:10

Lin


With ASP.NET MVC you can indeed bind your model on a GET request, as long as you have the same query string parameter names as of the property names of your Model class. Example from this answer:

public class ViewModel
{
  public string Name { set;get;}
  public string Loc{ set;get;}
}

You can do a Get request like this

MyAction?Name=jon&Loc=America

and MVC will automatically bind your model:

[HttpGet]
public ViewResult MyAction(ViewModel model)
{
    // Do stuff
    return View("ViewName", model);
}
like image 29
Beyers Avatar answered Oct 19 '22 15:10

Beyers