50

我有以下视图,它创建了 10 个 ajax.beginform ,但是我面临的问题是,如果在创建对象期间发生错误,那么尽管我已经设置了 ModelState.AddModelError 将不会显示在视图上该@Html.ValidationSummary(true) 视图如下所示

@model Medical.Models.VisitLabResult

@for (int item = 0; item < 10; item++)
{
    <tr id = @item>
    @using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions
    {
        HttpMethod = "Post",
        UpdateTargetId = item.ToString() + "td",
        InsertionMode = InsertionMode.Replace,
        LoadingElementId = "progress2",
        OnSuccess = string.Format(
            "disableform({0})",
            Json.Encode(item)),
    }))
    {  
        @Html.ValidationSummary(true)

        @Html.AntiForgeryToken()
        <td>
            @Html.DropDownList("LabTestID", String.Empty)
            @Html.ValidationMessageFor(model => model.LabTestID)
        </td>
        <td>
            @Html.EditorFor(model => model.Result)
            @Html.ValidationMessageFor(model => model.Result)
        </td>

        <td>
            @Html.EditorFor(model => model.DateTaken)
            @Html.ValidationMessageFor(model => model.DateTaken)
        </td>

        <td>
            @Html.EditorFor(model => model.Comment)
            @Html.ValidationMessageFor(model => model.Comment)
        </td>

        <td>
            <input type="submit" value="Create" />
        </td>

        <td id = @(item.ToString() + "td")>
        </td>
    }
    </tr>
    }
</table>

我定义 ModelState.AddModelError 的操作方法如下所示:-

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateAll(VisitLabResult vlr, int visitid = 28)
{
    try
    {
        if (ModelState.IsValid)
        {
            var v = repository.GetVisit(visitid);
            if (!(v.EligableToStart(User.Identity.Name))){ 
                return View("NotFound"); 
            }
            vlr.VisitID = visitid;
            repository.AddVisitLabResult(vlr);
            repository.Save();

            return Content("Addedd Succsfully");
        }
    }
    catch (DbUpdateException)
    {
        JsonRequestBehavior.AllowGet);
        ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
    }
}

那么我如何在我的视图上显示 ModelState.AddModelError 。

4

3 回答 3

74


我会敦促你改变你的try{ } catch(){ }

首先检查是否存在对给定 id 的访问,如果存在,则简单地返回带有添加模型错误的模型

    if (visitExists)
    {
         ModelState.AddModelError("CustomError", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
         return View(vlr);    
    }
    //Other code here

将您的 AddModelError 更改为

ModelState.AddModelError("CustomError", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");

在您看来,只需添加一个

@Html.ValidationMessage("CustomError")

然后,当您返回模型时,错误将显示在您放置 @Html.ValidationMessage 的位置...

于 2012-05-09T22:29:20.390 回答
10

@Html.ValidationSummary(true) shows only the error message about model's propertys, if you want to show also the added message, added with

ModelState.AddModelError(
    "CustomError", 
    "The Same test Type might have been already created, go back to the Visit page to see the avilalbe Lab Tests"); 

you need to set @Html.ValidationSummary(false) If you need to display the validation's message near your input fields you need to set @Html.ValidationSummary(true) and to follow the steps suggested by Syneryx

于 2013-05-16T16:26:00.813 回答
5

您可以在 View 中使用 from ViewDatadictionary 来访问ModelState数据。

例如:

在行动:

ModelState.AddModelError("CustomError", "Error 1");
ModelState.AddModelError("CustomError", "Error 2");

并获得“错误 1”消息:

ViewData.ModelState["CustomError"].Errors[0].ErrorMessage
于 2013-12-10T10:26:26.237 回答