C# 未绑定模型项时如何添加 ModelState.AddModelError 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12936604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add ModelState.AddModelError message when model item is not binded
提问by kk1076
I am new to MVC4. Here I added the ModelState.AddModelError message to display when the delete operation is not possible.
我是 MVC4 的新手。这里我添加了 ModelState.AddModelError 消息以在无法进行删除操作时显示。
<td>
<a id="aaa" href="@Url.Action("Delete", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID })" style="text-decoration:none">
<img alt="removeitem" style="vertical-align: middle;" height="17px" src="~/Images/remove.png" title="remove" id="imgRemove" />
</a>
@Html.ValidationMessage("CustomError")
</td>
@Html.ValidationSummary(true)
In my controller
在我的控制器中
public ActionResult Delete(string id, string productid)
{
int records = DeleteItem(id,productid);
if (records > 0)
{
ModelState.AddModelError("CustomError", "The item is removed from your cart");
return RedirectToAction("Index1", "Shopping");
}
else
{
ModelState.AddModelError(string.Empty,"The item cannot be removed");
return View("Index1");
}
}
Here I didnt pass any of the model item in the View to check for the item in Model and I couldnt get the ModelState error message ..
Any suggestions
在这里,我没有通过 View 中的任何模型项来检查 Model 中的项,并且我无法收到 ModelState 错误消息..
任何建议
采纳答案by VJAI
The ModelStateis created at each request so you should use TempData.
将ModelState在每个请求创建,所以你应该使用TempData。
public ActionResult Delete(string id, string productid)
{
int records = DeleteItem(id,productid);
if (records > 0)
{
// since you are redirecting store the error message in TempData
TempData["CustomError"] = "The item is removed from your cart";
return RedirectToAction("Index1", "Shopping");
}
else
{
ModelState.AddModelError(string.Empty,"The item cannot be removed");
return View("Index1");
}
}
public ActionResult Index1()
{
// check if TempData contains some error message and if yes add to the model state.
if(TempData["CustomError"] != null)
{
ModelState.AddModelError(string.Empty, TempData["CustomError"].ToString());
}
return View();
}
回答by Levi Botelho
RedirectToAction will clear ModelState. You must return a view in order to use this data. Therefore, the first "if" case won't work. Also, ensure that you have a control in your view (like ValidationSummary) which displays the error... this could be the problem in the second case.
RedirectToAction 将清除 ModelState。您必须返回一个视图才能使用此数据。因此,第一个“如果”情况将不起作用。此外,请确保您的视图中有一个控件(如 ValidationSummary)显示错误......这可能是第二种情况下的问题。
回答by Ryan Amies
The RedirectToAction method returns 302 which causes the client to be redirected. Because of this the ModelState is lost as the redirect is a new request. You could however, use the TempData property which allows you to store a temporary piece of data that is unique to the session. You could then check for this TempData on the other controller and add a ModelState error in that method.
RedirectToAction 方法返回 302,这会导致客户端被重定向。因此,由于重定向是一个新请求,因此 ModelState 丢失。但是,您可以使用 TempData 属性,该属性允许您存储会话唯一的临时数据。然后您可以在另一个控制器上检查这个 TempData 并在该方法中添加一个 ModelState 错误。

![C# Excel.Worksheet.Cells[row,col] =](/res/img/loading.gif)