首页 > 解决方案 > ValidationMessageFor 未显示某些字段?MVC 英孚

问题描述

我用于创建视图的控制器 ActionMethod 是,

  public ActionResult Create(int id=0)
        {
            Employee emp = new Employee();
                ViewBag.TribeId = new SelectList(db.Tribes, "TribeId", "TribeName");
            return View(emp);
        }

我有 EF 模型员工,即

   public partial class Employee
    {
        public int EmpIoyeeId { get; set; }

        [Display(Name = "FirstName", ResourceType = typeof(Resources.Resource))]
        [Required(ErrorMessageResourceType = typeof(Resources.Resource),
                  ErrorMessageResourceName = "FirstNameRequired")]

        public string FirstName { get; set; }

        [Display(Name = "Secondname", ResourceType = typeof(Resources.Resource))]
        [Required(ErrorMessageResourceType = typeof(Resources.Resource),
                  ErrorMessageResourceName = "SecondnameRequired")]

        public string Secondname { get; set; }

       [Display(Name = "TribeId", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "TribeIdRequired")]
       public int TribeId { get; set; }

      public virtual Tribe Tribe { get; set; }
}

这里,TribeId 是 Employee 表和 Employee EF 类中的 Fk

我的查看页面就像,

@model Structure.Employee

@using (Html.BeginForm()
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">

<div class="form-group">
                @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-sm-4" })
                <div class="col-sm-8">
                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                </div>
            </div>

 <div class="form-group">
                        @Html.LabelFor(model => model.Secondname, htmlAttributes: new { @class = "control-label col-sm-4" })
                        <div class="col-sm-8">
                            @Html.EditorFor(model => model.Secondname, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Secondname, "", new { @class = "text-danger" })
                        </div>
                    </div>

<div class="form-group">
                        @Html.LabelFor(model => model.TribeId, @Resource.TribeName, htmlAttributes: new { @class = "control-label col-sm-4" })

                        <div class="col-sm-8">
                            @Html.DropDownList("TribeId", null, htmlAttributes: new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.TribeId, "", new { @class = "text-danger" })
                        </div>
                    </div>

}

现在它适用于 FirstName 和 Second name 但我不明白为什么它没有显示 TribeId 的验证消息?我已经检查了资源文件以显示一切正常的验证消息。

希望得到您的建议。

谢谢

标签: entity-frameworkvalidationrazorasp.net-mvc-5

解决方案


这就是我为使 TribeId 验证工作所做的工作

首先在你的行动中

public ActionResult Create(int id=0)
{
    Employee emp = new Employee();
    ViewBag.Tribes = new SelectList(db.Tribes, "TribeId", "TribeName");
    return View(emp);
}

在您看来,请使用以下内容

<div class="form-group">
    @Html.LabelFor(model => model.TribeId, "tribe", htmlAttributes: new { @class = "control-label col-sm-4" })
    <div class="col-sm-8">
        @Html.DropDownListFor(model => model.TribeId, (IEnumerable<SelectListItem>)ViewBag.Tribes, "Select Tribe")
        @Html.ValidationMessageFor(model => model.TribeId, "", new { @class = "text-danger" })
    </div>
</div>

希望这可以帮助


推荐阅读