使用AJAX发布MVC模型? [英] Post an MVC model with AJAX?
问题描述
我可以使用AJAX发布MVC模型,例如:
Can I post an MVC model using AJAX, like:
$.ajax ({
//what elements are important?
data: '@Model.Product',
success: function(data){
$("#divProducts").html(data);
}
}
我想避免将模型更改为JSON或JavaScript对象,因为我仍然必须从页面上的字段中加载它们.如果我使用标准的@ Html.TextboxFor填充模型字段,然后使用AJAX发布整个模型,则会更容易.
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.
推荐答案
简单的答案是不使用JSON.Encode方法,而只是使用值填充加载的模型,然后将$ .serializeArray()与以下类一起使用您要用来加载模型的字段.
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.
这篇关于使用AJAX发布MVC模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!