Skip to content Skip to sidebar Skip to footer

Post An Mvc Model With Ajax?

Can I post an MVC model using AJAX, like: $.ajax ({ //what elements are important? data: '@Model.Product', success: function(data){ $('#divProducts').html(d

Solution 1:

You can serialize the model into JSON and then post the serialized JSON object to server.

var productModel= @Html.Raw(Json.Encode(Model.Product))
$.ajax ({
     //send the serialized JSON data: JSON.stringify(productModel),
     success: function(data){
        $("#divProducts").html(data);
     }
 }

Solution 2:

The simple answer is to not use the JSON.Encode method but to simply fill the loaded model with values and then use $.serializeArray() with the class of the fields you want to load the model with.

Post a Comment for "Post An Mvc Model With Ajax?"