我正在尝试使用 jquery ajax 调用 mvc 3 操作方法。请注意,我正在 ajax 中生成局部视图,并且在生成的局部视图中,我再次调用 ajax(它类似于嵌套的 ajax 调用)。这是我的代码:
部分观点:
@model MYWeb.UI.Models.ClientDetailsViewModel
视图中的javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#savedetails@(i)").click(function () {
var clientNumber = $('#ClientNumber@(i)').val();
var clientName = $('#ClientName@(i)').val();
var client =
{
ClientNumber: clientNumber,
ClientName: clientName
};
alert(JSON.stringify(client));
$.ajax({
url: '/Home/ClientUpdate',
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(client),
beforeSend: function () {
$("#loader@(i)_details").fadeIn('fast');
},
complete: function () {
$("#loader@(i)_details").fadeOut('fast');
},
success: function () {
$("#loader@(i)_details").fadeOut('fast');
if ($("#ModelFirstError").val())
showModelMessageError;
else
showUpdateMessageSuccess();
},
error: function () {
$("#loader@(i)_details").fadeOut('fast');
showUpdateMessageError();
},
async: true,
processData: false
});
});
});
模型对象:
public class ClientDetailsViewModel
{
[Display(ResourceType = typeof(adm), Name = "ClientNumber")]
public int ClientNumber { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessageResourceName = "RequiredClientName", ErrorMessageResourceType = typeof(adm))]
[Display(ResourceType = typeof(adm), Name = "ClientName")]
public string ClientName { get; set; }
more fields...
}
动作方法:
[Authorize]
[HttpPost]
public ActionResult ClientUpdate(ClientDetailsViewModel client)
{}
这种更新尝试总是返回 ajax 错误。我究竟做错了什么?
编辑:
我注意到,当我删除操作方法的参数时,它确实被调用了。但是我怎样才能通过这种方式传递模型呢?
[Authorize]
[HttpPost]
public ActionResult ClientUpdate()
{}