簡體   English   中英

ValidationMessageFor 未顯示某些字段? MVC 英孚

[英]ValidationMessageFor is not showing for some fields ? MVC EF

我的 Controller ActionMethod 創建視圖是,

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

我有 EF model 員工,

   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 表中的 Fk 和 Employee EF class

我的查看頁面就像,

@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>

}

現在它適用於名字和名字,但我不明白為什么它沒有顯示 TribeId 的驗證消息? 我已經檢查了資源文件以顯示一切正常的驗證消息。

希望得到您的建議。

謝謝

這就是我為使 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>

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM