我的MVC应用程序中有一个更大的Web表单。当我单击“保存”按钮时,将提交表单并保存值,最后,我将重定向到同一页面/表单。但是,当已经加载并且不需要时,它将重新呈现整个表单。我该如何避免呢?我希望“保存”按钮的行为类似于:保存所有内容,然后继续原样。
我的控制器代码:
[HttpPost]
[ValidateInput(false)]
public ActionResult FormCol(FormCollection collection)
{
...
if (Request.Form["DocumentId"] != null)
{
...
return RedirectToAction("FormCol", new { id = DocumentId });
}
看法:
<input type="hidden" value="@document.Id" name="DocumentId" />
您将需要通过Ajax / jquery发布表单:
$.ajax({
url: '/someController/FormCol?variable1=' + $("#input1").val() + '&variable2=' + $("#input2").val(),
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data); //This is where you would do something based upon a successful save, such as alerting the user to their new document ID or something.
},
error: function () {
alert("error"); //This is where you know that your save failed.
}
});
并更改您的Controller操作返回Json:
public JsonResult FormCol(string variable1, string variable2)
{
//do saving stuff here
return Json(new { id = DocumentId });
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句