For the unobtrusive client validation you could subscribe to the
showErrors
callback and display the errors the way you want (alert
in your case):(function ($) {
$.validator.setDefaults({
onsubmit: true,
onkeyup: false,
onfocusout: false,
showErrors: function (errorMap, errorList) {
if (errorList.length > 0) {
var errors = errorList.map(function (element) {
return element.message;
}).join('\r\n');
alert(errors);
}
}
});
})(jQuery);
In order to display server side errors with
alert
on the client you shouldn't be using the @Html.ValidationSummary( true )
helper because this helper outputs the errors directly in the view and you said that you don't have enough place.
So you could emit javascript dynamically when there are errors and display them in an
alert
:@if (!ViewData.ModelState.IsValid)
{
<script type="text/javascript">
var errors = @Html.Raw(
Json.Encode(
string.Join(
Environment.NewLine,
ViewData.ModelState
.Where(x => x.Value.Errors.Count > 0)
.SelectMany(x => x.Value.Errors)
.Select(error => error.ErrorMessage))
)
);
alert(errors);
</script>
}
Ref : http://stackoverflow.com/questions/12028825/how-to-display-validation-messages-with-javascript-alert-window
Comments
Post a Comment