MVC4 - When Passing Complex model to the Controller using Ajax

Ayo Adesina

This is a Java Script function that is called when a user clicks add to basket, I am testing out my Ajax call before I put in all my logic to build the correct values form my form, (so hard coded for now.)

    function AddItem() {

    var myproduct = { Id: 1, Price: 17, ProductName:"product name" }

    var cartItem = { Quantity: 1, Product: myproduct };


    $.ajax({
        url: "/Cart/AddItem",
        type: 'post',
        data: cartItem,
        success: function (data) {
            if (data.IsSuccess) {
                alert("test Hello Success");
            }

            alert("test HELLO Fail");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown + "- Error");
        }
    });
}

The problem is when testing this Java Script my strongly typed parameter inside my controller action only has some of the values set, My controller action looks like this:

[HttpPost]
    public JsonResult AddItem(CartItem cartItem)
    {  
        cartItem.Cart = cartService.GetCartCurrent(this);
        cartService.SaveCartItem(cartItem);

       //bla bla bla

     }

The Cart Item definition looks like this:

[Serializable]
public class CartItem
{
    [Key]
    public int Id { get; set; }
    public virtual Product Product { get; set; }
    public virtual List<SelectedProductOption> SelectedProductOptions { get; set; }
    public virtual Cart Cart { get; set; }
    public int Quantity { get; set; }

}

So when I set a break-point CartItem.Quaintity = 1 but CartItem.Product is NOT NULL but the values for Product.Id = 0 and Product.Price is 0.

Why??

How do you set the inner object of the complex model.

Any ideas?

Lokesh Suthar

The problem seems to that your data is not being deserialized correctly. You can use JavaScriptSerializer to solve this.

Here

  var myproduct = { Id: 1, Price: 17, ProductName: "product name" };

            var cartItem = { Quantity: 1, Product: myproduct };


            $.ajax({
                url: "/Home/Index",
                type: 'post',
                data: "formData=" + JSON.stringify(cartItem),
                success: function (data) {
                    if (data.IsSuccess) {
                        alert("test Hello Success");
                    }

                    alert("test HELLO Fail");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(errorThrown + "- Error");
                }
            });

[HttpPost]
    public JsonResult AddItem(string formData)
    {  
        var js = new JavaScriptSerializer();
        CartItem cartItem = js.Deserialize<CartItem>(formData);

        cartItem.Cart = cartService.GetCartCurrent(this);
        cartService.SaveCartItem(cartItem);

       //bla bla bla

     }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing complex model to controller using targeted AJAX and ASP

From Dev

How to pass a complex view model into a controller action via ajax call with JSON in .Net MVC4?

From Dev

ASP.NET MVC passing whole model to controller using ajax

From Dev

AJAX POST Complex JSON to MVC4 Controller

From Dev

MVC4 Complex Object is null when bound from view to controller

From Dev

Passing localstorage to controller using Ajax

From Dev

Passing localstorage to controller using Ajax

From Dev

getting null value in list when passing by ajax call to mvc controller

From Dev

Passing data to a controller with $.ajax (MVC 4)

From Dev

MVC passing Model to Controller issue

From Dev

Passing A List Of Objects Into An ActionResult MVC Controller Method Using jQuery Ajax

From Dev

passing an array of int to MVC controller using ajax javascript

From Dev

Passing value from MVC View to Controller using ajax

From Dev

MVC 4 ajax Json passing model

From Dev

MVC4 WebGrid loaded from Ajax form - multiple calls to Controller when sorting and paging

From Dev

Sending selected value of Drop Down List back to a Controller Action using jQuery Ajax MVC4

From Dev

Vanilla JS MVC -- Notify controller from model when AJAX succeeds

From Dev

View not rendering when passing ajax data to controller

From Dev

MVC4 Model : Using a List<> as a Property

From Dev

Laravel passing data using ajax to controller

From Dev

Reading and passing element innerhtml to controller using ajax?

From Dev

Passing a string to an object in controller using ajax

From Dev

Passing form data to a controller method using Ajax

From Dev

Passing two models to Controller using Ajax BeginForm()

From Dev

Passing a string to an object in controller using ajax

From Dev

Passing a JSON Array of Objects to Controller using AJAX

From Dev

Value not passing from view to controller using ajax

From Dev

MVC4 passing same parameters to multiple actions on controller

From Dev

Passing values from the view to the controller in MVC4

Related Related

  1. 1

    Passing complex model to controller using targeted AJAX and ASP

  2. 2

    How to pass a complex view model into a controller action via ajax call with JSON in .Net MVC4?

  3. 3

    ASP.NET MVC passing whole model to controller using ajax

  4. 4

    AJAX POST Complex JSON to MVC4 Controller

  5. 5

    MVC4 Complex Object is null when bound from view to controller

  6. 6

    Passing localstorage to controller using Ajax

  7. 7

    Passing localstorage to controller using Ajax

  8. 8

    getting null value in list when passing by ajax call to mvc controller

  9. 9

    Passing data to a controller with $.ajax (MVC 4)

  10. 10

    MVC passing Model to Controller issue

  11. 11

    Passing A List Of Objects Into An ActionResult MVC Controller Method Using jQuery Ajax

  12. 12

    passing an array of int to MVC controller using ajax javascript

  13. 13

    Passing value from MVC View to Controller using ajax

  14. 14

    MVC 4 ajax Json passing model

  15. 15

    MVC4 WebGrid loaded from Ajax form - multiple calls to Controller when sorting and paging

  16. 16

    Sending selected value of Drop Down List back to a Controller Action using jQuery Ajax MVC4

  17. 17

    Vanilla JS MVC -- Notify controller from model when AJAX succeeds

  18. 18

    View not rendering when passing ajax data to controller

  19. 19

    MVC4 Model : Using a List<> as a Property

  20. 20

    Laravel passing data using ajax to controller

  21. 21

    Reading and passing element innerhtml to controller using ajax?

  22. 22

    Passing a string to an object in controller using ajax

  23. 23

    Passing form data to a controller method using Ajax

  24. 24

    Passing two models to Controller using Ajax BeginForm()

  25. 25

    Passing a string to an object in controller using ajax

  26. 26

    Passing a JSON Array of Objects to Controller using AJAX

  27. 27

    Value not passing from view to controller using ajax

  28. 28

    MVC4 passing same parameters to multiple actions on controller

  29. 29

    Passing values from the view to the controller in MVC4

HotTag

Archive