ASP.Net MVC Model Binding Complex Object using GET

Phil Sandler

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?

Lin

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Asp.net- Mvc Complex Model Binding

From Dev

MVC manually serializing complex object or model binding

From Dev

MVC manually serializing complex object or model binding

From Dev

Complex ASP.NET MVC Binding

From Dev

asp.net mvc 6 model binding to complex collection - IList<T>

From Dev

ASP.NET MVC Complex Model Updating

From Dev

ASP.MVC Settings Object Model Binding

From Dev

Asp.net MVC binding to view model

From Dev

Json and ASP.NET MVC Model Binding

From Dev

GUID model binding in ASP.NET MVC

From Dev

ASP.NET MVC Model binding not parsing

From Dev

What is model binding in ASP.NET MVC?

From Dev

ASP.Net MVC Model binding for empty input string creates empty model object

From Dev

MVC 6 complex model binding

From Dev

MVC Model binding of complex objects

From Dev

Using Linq to get value from a model asp.net mvc

From Dev

ASP.NET MVC Model bindING separate day, month, year string fields to single DateTime object

From Dev

ASP.NET MVC 4 JSON Binding to the View Model - Nested object error

From Dev

ASP.NET MVC Model bindING separate day, month, year string fields to single DateTime object

From Dev

Asp.Net MVC - Model Binding to Model or ViewModel

From Dev

ASP.NET MVC Model binding doesn't work with AJAX GET but works with Post

From Dev

ASP.NET MVC: Submit a list using Model binding after removing an input from the middle

From Dev

How to get the object properties using AngularJS and ASP.NET MVC

From Dev

How to pass a complex model back to the controller in asp.net mvc

From Dev

ASP.NET MVC Model Binding Issue with Nested Collections

From Dev

ASP.NET MVC Model Binding With ListBoxFor & DropDownListFor Helpers

From Dev

Autofac "action injection" with ASP.NET MVC model binding

From Dev

ASP.NET MVC Model Binding and Validation Order

From Dev

ASP.NET MVC Model Binding with jQuery ajax request for collection

Related Related

  1. 1

    Asp.net- Mvc Complex Model Binding

  2. 2

    MVC manually serializing complex object or model binding

  3. 3

    MVC manually serializing complex object or model binding

  4. 4

    Complex ASP.NET MVC Binding

  5. 5

    asp.net mvc 6 model binding to complex collection - IList<T>

  6. 6

    ASP.NET MVC Complex Model Updating

  7. 7

    ASP.MVC Settings Object Model Binding

  8. 8

    Asp.net MVC binding to view model

  9. 9

    Json and ASP.NET MVC Model Binding

  10. 10

    GUID model binding in ASP.NET MVC

  11. 11

    ASP.NET MVC Model binding not parsing

  12. 12

    What is model binding in ASP.NET MVC?

  13. 13

    ASP.Net MVC Model binding for empty input string creates empty model object

  14. 14

    MVC 6 complex model binding

  15. 15

    MVC Model binding of complex objects

  16. 16

    Using Linq to get value from a model asp.net mvc

  17. 17

    ASP.NET MVC Model bindING separate day, month, year string fields to single DateTime object

  18. 18

    ASP.NET MVC 4 JSON Binding to the View Model - Nested object error

  19. 19

    ASP.NET MVC Model bindING separate day, month, year string fields to single DateTime object

  20. 20

    Asp.Net MVC - Model Binding to Model or ViewModel

  21. 21

    ASP.NET MVC Model binding doesn't work with AJAX GET but works with Post

  22. 22

    ASP.NET MVC: Submit a list using Model binding after removing an input from the middle

  23. 23

    How to get the object properties using AngularJS and ASP.NET MVC

  24. 24

    How to pass a complex model back to the controller in asp.net mvc

  25. 25

    ASP.NET MVC Model Binding Issue with Nested Collections

  26. 26

    ASP.NET MVC Model Binding With ListBoxFor & DropDownListFor Helpers

  27. 27

    Autofac "action injection" with ASP.NET MVC model binding

  28. 28

    ASP.NET MVC Model Binding and Validation Order

  29. 29

    ASP.NET MVC Model Binding with jQuery ajax request for collection

HotTag

Archive