How know if ModelState contains errors

a little addition to @Dimitrov answer:

<script type="text/javascript">
    var isValid = '@Html.Raw(Json.Encode(ViewData.ModelState.IsValid))';

    if (isValid != 'true')
        // model has some errors...
</script>

It's important to use single qoutes around the helper. Otherwise, that ending semicolon ; cause problems. Nether you can write it, nor you can't, at all cases it cause a syntax error. Unless you put those single quotes around the helper as I mentioned.


You could spit global javascript variable:

<script type="text/javascript">
    var isValid = @Html.Raw(Json.Encode(ViewData.ModelState.IsValid));
</script>

and then:

$(function() {
    if (!isValid) {
        alert('opa');
    }
});