entity-framework - 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 的验证消息?我已经检查了资源文件以显示一切正常的验证消息。
希望得到您的建议。
谢谢
解决方案
这就是我为使 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>
希望这可以帮助
推荐阅读
- angularjs - 流星(1.6)如何使用离线图片大气包(chrisben:imgcache)
- xamarin.forms - Xamarin Forms:无法将命令参数值从 XAML 传递到 ViewModel
- c# - 如何将计算值添加到此 TryUpdateModelAsync<>
- powerbi - Power BI - 最大日期或所选日期的最近日期
- html - 从 Word 文档编码 PDF 友好的 HTML 页面
- angular - 可以从 Windows 和 Web 客户端访问作为 Windows 服务托管的 WCF 服务吗?
- http - 在golang http包中拆分http请求头有意义吗?
- ruby-on-rails - 没有路线匹配 {:action=>"show", :controller=>"carts", :id=>nil},缺少必需的键:[:id]
- python - 如何修复错误消息:“chromedriver.exe”可执行文件可能具有错误的权限
- python - 为什么在使用 urlopen() 时出现回溯错误?