0
在提交表單後遇到驗證錯誤,並且希望重定向回表單但希望URL反映表單的URL而不是頁面的情況之一行動。RedirectToAction()丟失請求數據
如果我使用viewData參數,我的POST參數將更改爲GET參數。
如何避免它? 我希望這個選項沒有改變爲GET參數。
在提交表單後遇到驗證錯誤,並且希望重定向回表單但希望URL反映表單的URL而不是頁面的情況之一行動。RedirectToAction()丟失請求數據
如果我使用viewData參數,我的POST參數將更改爲GET參數。
如何避免它? 我希望這個選項沒有改變爲GET參數。
正確的設計模式不是在驗證錯誤的情況下重定向bu再次呈現相同的表單。只有在操作成功時,您才應該重定向。
例子:
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
// some validation error occurred => redisplay the same form so that the user
// can fix his errors
return View(model);
}
// at this stage we know that the model is valid => let's attempt to process it
string errorMessage;
if (!DoSomethingWithTheModel(out errorMessage))
{
// some business transaction failed => redisplay the same view informing
// the user that something went wrong with the processing of his request
ModelState.AddModelError("", errorMessage);
return View(model);
}
// Success => redirect
return RedirectToAction("Success");
}
這種模式可讓您保存在發生某些錯誤情況下,所有模型值,你需要重新顯示了同樣的觀點。
謝謝!剛剛發現這個話題類似的問題http://stackoverflow.com/questions/1936/how-to-redirecttoaction-in-asp-net-mvc-without-losing-request-data –