[英]mvc Ajax How to upate serialized model with formCollection
I have a page, which will send back the serialzed Model to the controller. 我有一个页面,该页面会将序列化的模型发送回控制器。 In this case I'm using ajax.
在这种情况下,我正在使用ajax。
In the View I have the following code: 在视图中,我有以下代码:
@Html.Hidden("processViewModel", new Microsoft.Web.Mvc.MvcSerializer().Serialize(Model, SerializationMode.Signed))
And the controller looks like this: 控制器看起来像这样:
[HttpPost]
public ActionResult PreviewSample([Deserialize]ProcessViewModel processViewModel /*FormCollection form*/)
and here is my ajax code: 这是我的ajax代码:
function GetPreview() {
var json = $('form').serializeObject();
complete = false;
json['ProcessDTO.FileUploadDTO.TempUploadFileDTO.FileInformation.HasHeader'] = false; // $('input[name="ProcessDTO.FileUploadDTO.TempUploadFileDTO.FileInformation.HasHeader"]').is(':checked');
alert($('input[name="ProcessDTO.FileUploadDTO.TempUploadFileDTO.FileInformation.HasHeader"]').is(':checked'));
var jsonData = JSON.stringify(json);
var url = ROOT + '/request/previewsample/';
if (lastPreviewCall != undefined) {
if (lastPreviewCall.status != 200) {
lastPreviewCall.abort();
}
}
lastPreviewCall = $.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
// dataType: 'json',
data: jsonData,
otherParam: true,
async: true,
success: function (res) {
$('#preview').find('tbody').find('tr').remove();
var selected = $('.addressType:checked').attr('data-hideClass');
for (var i = 0; i < res.PreviewData.length; i++) {
var entry = '<tr>';
var row = res.PreviewData[i];
for (var u = 0; u < row.length; u++) {
if (u == 0 && selected == 'consumer') {
entry += '<td></td>';
}
else {
entry += '<td><span>' + row[u] + '</span></td>';
}
}
entry += '</tr>';
$('#preview').find('tbody').append(entry)
}
}
});
} }
So now I have a checkbox, which is default true in this model and when I uncheck it, the ajax will send the serialized model back to the controller, but the checkbox has always the value true. 因此,现在我有了一个复选框,此模型默认为true,当我取消选中该复选框时,ajax会将序列化的模型发送回控制器,但是此复选框始终具有true值。
how can i update the model? 我如何更新模型?
Thanks for any help. 谢谢你的帮助。
The solution is to add 解决方法是添加
TryUpdateModel(processViewModel);
in the controller, and then is everything fine... 在控制器中,然后一切都很好...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.