Asp.net mvc 4 ValidationMessage无法按预期工作
我试图在我的视图上添加验证。但我无法访问@Html.ValidationMessageFor内的属性 我的看法Asp.net mvc 4 ValidationMessage无法按预期工作,asp.net-mvc-4,validation,Asp.net Mvc 4,Validation,我试图在我的视图上添加验证。但我无法访问@Html.ValidationMessageFor内的属性 我的看法 @model IEnumerable<Entity.Employee> <div class="jumbotron"> @using (Html.BeginForm("Create", "Employee", FormMethod.Post)) { <label>Name</label>
@model IEnumerable<Entity.Employee>
<div class="jumbotron">
@using (Html.BeginForm("Create", "Employee", FormMethod.Post))
{
<label>Name</label>
<input id="txtName" type="text" name="EmployeeName" class="btn btn-default" />
@Html.ValidationMessageFor(model => model. //not able to get the Name property
<input type="submit" value="Save" class="btn btn-primary" />
}
</div>
控制器
[HttpPost]
public ActionResult Create(Employee employee, string EmployeeName)
{
if (ModelState.IsValid)
{
employee.Name = EmployeeName;
repository.SaveRole(role);
return RedirectToAction("Index");
}
else
{
return View(employee);
}
}
我不确定我遗漏了什么,或者是因为视图与IEnumerable强耦合您的视图定义了IEnumerable类型的模型。这代表多个员工。您应该将模型声明更改为
@model Entity.Employee
我无法执行foreach语句,因为代码中没有显示foreach。如果您想在同一个视图中创建新员工,那么您应该创建一个包含“新员工”字段和“员工列表”的视图模型。您可以在回答中简单演示一下。
@model Entity.Employee