【发布时间】:2013-07-22 00:25:41
【问题描述】:
我有一个场景,比如我需要在编辑 [GET] 请求时显示来自 DB 的错误消息。
我知道如果请求类型是 [POST] 可以做到这一点,但是我们如何在 [GET] 请求中做到这一点。
相同的代码:
[HttpGet] 公共 ActionResult 编辑(Int64 ID) { tblSample1 模型 = 获取数据(ID); ViewData.ModelState.AddModelError(model.Username, "提供的用户名无效。"); return View("~/Views/Sample1/_Edit.cshtml", model); }[HttpPost]
public ActionResult Edit(tblSample1 model)
{
if (ModelState.IsValid)
{
......
......
}
}
【问题讨论】:
-
为什么对此类验证进行完整回发以及为什么不通过 json 返回验证
-
我认为这个链接可能对你有帮助:stackoverflow.com/questions/5739362/…
标签: c# asp.net-mvc-3 data-annotations modelstate