ModelState.AddModelError未显示我的观点里 [英] ModelState.AddModelError is not being displayed inside my view

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

问题描述

我有以下的看法等,其中创造10 ajax.beginform ,,而是我所面临的问题是,那么ModelState.AddModelError不会对视图虽然显示创建对象的过程中发生柜面错误我已经设置了 @ Html.ValidationSummary(真)
视图看上去如下

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对我的看法。

推荐答案



我敦促你改变你的尝试{}赶上(){}

和第一次检查,如果存在给定id的访问
如果是简单地返回模型与添加的模型误差

And first check if there exists a visit for the given id and 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未显示我的观点里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆