创建视图的我的控制器操作方法是
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表和EmployeeEF类中的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和Secondname工作,但我不明白为什么它没有显示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>
希望这能帮助