c# - Show Server-Side Validation Message in ValidationMessageFor Block -
i'm using variation on standard mvc membership authentication.
given model has:
[required] [display(name = "user name")] public string username { get; set; }
and view has:
<div class="form-field"> @html.editorfor(m => m.username) @html.validationmessagefor(m => m.username) </div>
if tab off user name field, validation message correctly shows "the user name field required.".
what need if username entered user fails further validation performed in actionresult submit, want display custom validation message.
an example user has entered username logged system (which checked on submit), validationmessage should display "user logged in via workstation xyz".
i suppose example of using customvalidationattribute within razor mvc 4 best, believe way work. thing is, seems client-side validation. in case, want server-side.
a little bit of further searching showed me barking wrong tree. custom server-side validation messages, following:
if (result.alreadyloggedin) { modelstate.addmodelerror("username", string.format("user logged in via workstation {0}", result.workstationname)); }
Comments
Post a Comment