c# - View not showing ValidationSummary and ValidationMessages -
i have view:
@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 */ } and controller:
[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); } and model:
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 working. if leave eg. name empty modelstate not valid in controler. validation messages not shown. if uncomment line: modelstate.addmodelerror("", "niepoprawne dane"); model error shown in view.
where mistake in code?
it's because using @html.validationsummary(true) means excludepropertyerrors = true
Comments
Post a Comment