I'm using a variation on the standard MVC membership authentication.
Given that my model has:
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
And that the View has:
<div class="form-field">
@Html.EditorFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
If I tab off the user name field, the validation message correctly shows "The User name field is required.".
What I need is that if the username entered by the user fails further validation that is performed in the ActionResult for submit, I want to display a custom validation message.
An example could be that the user has entered a username that is already logged into the system (which would only be checked on submit), so the ValidationMessage should display something like "User already logged in via workstation xyz".
I suppose a good example of using a CustomValidationAttribute within Razor MVC 4 would be best, as I believe that is the only way it could work. The only thing is, this seems to be for client-side validation. In my case, I only want server-side.
A little bit of further searching showed me I was barking up the wrong tree. For custom server-side validation messages, just do something like the following:
if (result.AlreadyLoggedIn)
{
ModelState.AddModelError("UserName", String.Format("User already logged in via workstation {0}", result.WorkstationName));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With