查看未显示ValidationSummary和ValidationMessages

时间:2014-03-11 09:44:35

标签: c# asp.net-mvc entity-framework

我有一个观点:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="input-group">
            <div class="input-group-addon">
                @Html.Label("Employee number", new { @class = "control-label" })
            </div>
            <div class="a">
                @Html.TextBoxFor(model => model.EmployeeNo, new {@class="form-control" })
                @Html.ValidationMessageFor(model => model.EmployeeNo)
            </div>
        </div>
       /*
       * other fields
       */
}

和控制器:

[HttpPost]
public ActionResult Edit([Bind(Include="Id,EmployeeNo,Name,Surname,ContactInfo,RoleId")] User user)
{
    ValidateRequestHeader(Request);
    if (ModelState.IsValid)
    {
        unitOfWork.userRepository.Update(user);
        unitOfWork.Save();
        return Json(new { ok = true, newurl = Url.Action("Index") });
    }
    //ModelState.AddModelError("", "Niepoprawne dane");
    ViewBag.RoleId = new SelectList(unitOfWork.roleRepository.Get(), "Id", "RoleName", user.RoleId);
    return PartialView(user);
}

和型号:

public partial class User
{
    public User()
    {
        this.DeviceUsages = new HashSet<DeviceUsage>();
    }

    public int Id { get; set; }
    [Required(ErrorMessage="Brak numeru pracownika")]
    public string EmployeeNo { get; set; }
    [Required(ErrorMessage = "Brak imienia")]
    public string Name { get; set; }
    [Required(ErrorMessage = "Brak nazwiska")]
    public string Surname { get; set; }
    [Required(ErrorMessage = "Brak Adresu email")]
    public string ContactInfo { get; set; }
    public int RoleId { get; set; }
}

Data annotations正在运作。如果我离开,例如。名称为空ModelState在控制器中无效。但是没有显示验证消息。如果我取消注释这一行: ModelState.AddModelError("", "Niepoprawne dane");这将是视图中显示的唯一Model error

我的代码中哪个错误?

1 个答案:

答案 0 :(得分:3)

这是因为您正在使用@Html.ValidationSummary(true)表示excludePropertyErrors = true