用于创建视图的我的控制器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>
}
现在它可以用于名字和名字,但是我不明白为什么它不显示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>
希望这会有所帮助