How to add an error message to the ModelState on MVC using Javascript?

This is my Model property

 public ModelStateDictionary modelSateClientSide { get; set; }

Now I called the Property in JavaScript and add the error in my ModelState

  • How to make ExtJs DatePicker in my MVC site?
  • ASP.NET MVC Partial view ajax post?
  • “Uncaught SyntaxError: Unexpected token import” when using momentjs with ASP5/MVC6
  • Is this javascript code pattern is good?
  • vuejs templates als partialviews, good practice?
  • Disable the button after clicking the button once
  • if (parseInt(academicAchievement, 10) > parseInt(peAcademicAchievement, 10))
        @Model.modelSateClientSide.AddModelError(string.Empty, "xxx");
        return false;

    Am I doing it correctly? And I got a error in above line:

    The best overloaded method match for
    has some invalid arguments.

    How can solve this? Any other option to add error message in ModelState on MVC using Javascript?

  • jQuery UI DatePicker Hangs on second call
  • The JS file doesn't work in partial view after it refreshed
  • How to get google place api results from autocomplete.getPlace()
  • jQuery Ajax with ASP.NET MVC Action: Passing arguments from JavaScript in POST
  • ASP.Net MVC3: Place .js files near View instead of Scripts folder
  • Call MVC 3 Client Side Validation Manually for ajax posts
  • One Solution collect form web for “How to add an error message to the ModelState on MVC using Javascript?”

    Well, I don’t think you can write to the ModelState Dictionary using JavaScript because it is a Client-Side language and C# / Razor are processed on the Server-Side.

    In the code you posted, the line

    @Model.modelSateClientSide.AddModelError(string.Empty, "xxx");

    Will actually be evaluated every time the page loads because it is processed on the server, and Razor doesn’t “know” about JavaScript or its conditional statements.

    If you, however, just want to display an error message similar to what the ValidationSummary HtmlHelper generates by default, you can write a JavaScript function to do it. Something more or less like this:

    function showClientError(message) {
       var $div = $('.validation-summary-errors');
       if ($div.length == 0) {
           $div = $('<div class="validation-summary-errors">');
           // Put the $div somewhere