当模型在后期操作中无效时,我想返回到带有错误消息以及查询字符串
的同一页面
我的代码在这里
public ActionResult Action1(string Key)
{
// do something
}
[HttpPost]
public ActionResult Action1(Model user)
{
if (ModelState.IsValid)
{
// do some stuff here
}
else
// redirect to same page with query string key and also error message
}
请通过显示错误消息建议我在模型无效时需要添加的行以留在同一页面中。
最佳答案
public ActionResult Action1(Model user)
{
if (ModelState.IsValid)
{
// all is okay
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
关于c# - 将查询字符串传递给 mvc View 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33476511/