ValidationMessageFor无法按预期工作

时间:2014-08-09 13:58:05

标签: asp.net-mvc-4 validation

我正在尝试在我的View上添加验证。但是我无法访问@Html.ValidationMessageFor(?)

中的属性

我的观点

@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>

员工类

[Required(ErrorMessage="Please enter name")]
public string Name { get; set; }

控制器

[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<Type>

结合在一起

1 个答案:

答案 0 :(得分:0)

您的视图已定义类型IEnumerable<Entity.Employee>的模型。这代表多名员工。您应该将模型声明更改为

@model Entity.Employee