[英]form submit and redirect to same page with no re render
I have a huger web form in my MVC app. 我的MVC应用程序中有一个较大的Web表单。 When I click my "save" button, the form is submitted and the values saved, and at the end I redirect to the same page/form. 当我单击“保存”按钮时,表单将被提交并保存值,最后我将重定向到同一页面/表单。 But it re renders the whole form, when it was already loaded and there is no need. 但它重新呈现整个表单,当它已经加载并且没有必要时。 How can I avoid that? 我怎么能避免这种情况? I want the SAVE button to behave like: save all but continue where I was. 我希望SAVE按钮表现得像:保存所有,但继续我的位置。
My controller code: 我的控制器代码:
[HttpPost]
[ValidateInput(false)]
public ActionResult FormCol(FormCollection collection)
{
...
if (Request.Form["DocumentId"] != null)
{
...
return RedirectToAction("FormCol", new { id = DocumentId });
}
View: 视图:
<input type="hidden" value="@document.Id" name="DocumentId" />
You will need to post your form via Ajax / jquery: 您需要通过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.
}
});
And change your Controller action return Json: 并更改您的Controller操作返回Json:
public JsonResult FormCol(string variable1, string variable2)
{
//do saving stuff here
return Json(new { id = DocumentId });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.