我是MVC4的新手。在這裏,我添加了ModelState.AddModelError消息,以便在刪除操作不可用時顯示。
如何在未綁定模型項時添加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)
在我的控制器
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");
}
}
在這裏我沒有通過任何模型中項目的視圖,以檢查模型中的項目,我不可能得到的ModelState錯誤消息。
任何建議