我试图用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
});
});
});
Model对象:
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错误。我做错了什么?
修改
我注意到当我将参数移除到action方法时,它会被调用。 但是我怎么能以这种方式传递模型呢?
[Authorize]
[HttpPost]
public ActionResult ClientUpdate()
{}