Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can I post an MVC model using AJAX, like:

 $.ajax ({
     //what elements are important?
     data: '@Model.Product',
     success: function(data){
        $("#divProducts").html(data);
     }
 }

I want to avoid changing the model into JSON or JavaScript objects because I would still have to load them from the fields on the page. It would be easier if I use the standard @Html.TextboxFor stuff to fill the model fields and then post the whole model with AJAX.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
     }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...