ValidationMessageFor не отображается для некоторых полей? MVC EF - PullRequest
0 голосов
/ 28 апреля 2020

Мой контроллер ActionMethod Для создания View это,

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

У меня есть модель EF Employee,

   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 - это Fk в таблице Employee и класс EF сотрудника

моя страница просмотра похожа,

@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 ? Я уже проверил файл ресурсов для отображения сообщения проверки, что все в порядке.

Надеемся на ваши предложения.

Спасибо

1 Ответ

0 голосов
/ 30 апреля 2020

Это то, что я сделал для того, чтобы проверка 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>

Надеюсь, это поможет

...