未显示在我的视图中

未显示在我的视图中

本文介绍了ModelState.AddModelError 未显示在我的视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following view,, which create 10 ajax.beginform ,, But the problem that i am facing is that incase an error occurs during the creation of the object then the ModelState.AddModelError will not be shown on the view although i have set the @Html.ValidationSummary(true)The view looks as follow

@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 的操作方法如下所示:-

And my action method which defines the ModelState.AddModelError looks as follow:-

[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.

推荐答案


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

首先检查是否存在对给定 id 的访问如果是这样,只需返回带有添加模型错误的模型

And first check if there exists a visit for the given idand if so simply returns the model with the added model error

    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 更改为

Change your AddModelError To

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

在您的视图中只需添加一个

And in your view simply add a

@Html.ValidationMessage("CustomError")

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

Then when you return your model the error will be shown where you have placed the @Html.ValidationMessage ...

这篇关于ModelState.AddModelError 未显示在我的视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:22