Praveen Kumar Vadige
Praveen Kumar Vadige

Reputation: 39

Jquery to Check ModelState is Valid or not

I want to check whether the ModelState is Valid or not through jquery. In My scenario if he click on submit button, and the fields fulfilled the requirement of Required Attributes, i want to open model popover, otherwise we want to show the errors, can any one tell me how to check ModelState is valid or not?

Upvotes: 1

Views: 13576

Answers (5)

Narek Terhovanesian
Narek Terhovanesian

Reputation: 1

You can also use:

 @ViewData.ModelState.IsValid

// In your JS define:
var True = true;
 var False = false;

function ClosePopup() {               
        if(@ViewData.ModelState.IsValid)
            close();   // will close the popup                   
}

Upvotes: 0

shivani
shivani

Reputation: 1028

When you use Data Annotation as your view validation , then you are not achieve validation using jquery , so you need to check first that all field are valid or not

using

$("form").isValid() 

Or

$("form").valid()

It is not working in my application , So I find one solution like that

When you using validation through model in your html side It will create one span with data annotation massages, that is

<span class="field-validation-valid text-small text-danger" data-valmsg-for="Name" data-valmsg-replace="true"></span>

It has two different class name
1. field-validation-valid
2. field-validation-error

so you can identify that , if your span class is field-validation-valid than all validation are true & if your span class is field-validation-error than its indicate that some validation is fail

so using that identification you can check your isValid() of form

 //Check all validation done 
function checkFormValidateOrNot() {

        if ($(".field-validation-error").length > 0) {
             return false;
           }

        $(".form-control").each(function () {
            if ($(this).attr("data-val") == "true" && $(this).val() == "" && 
                $(this).is("select") == false) {
                   return false;
               }
        });

     return true;
}

//Trigger when form sumbit for save
$("form").on("submit", function () {

    //check validate if all field are validate true
    if (checkValidateOrNot() == true) {
        MakeReadOnly();
        alert("Pass all validation");
    }
});

this way I check ModelState is Valid using jquery

Upvotes: 2

serene
serene

Reputation: 685

For this I think you have two ways of working around. First you can simply make a ajax post to your controller (say:

public JsonResult IsModelStateValid(YourModel model)
{
  return Json(ModelState.IsValid);
}

and based on this you can do whatever you want. Second, if you don't want to make a server call, you can simply check

var isValid = $('#frm').valid()

in your jquery submit event or button click event. This will work if you have enabled unobtrusive validation and works for all in built validations of ASP.NET MVC, but not for your custom validation.

Upvotes: 4

Vibhesh Kaul
Vibhesh Kaul

Reputation: 2613

Convert the Model to a JSON object and iterate through it to check and validate the model properties.

 <script type="text/javascript">
        var model = @Html.Raw(Json.Encode(Model));

    // Now iterate through the JSON object and check for the properties you want to validate

 </script>

Upvotes: 0

Archil Labadze
Archil Labadze

Reputation: 4325

Here is two ways: First: Make validation via JS HTML on page and display values, bun if you want display Model errors you need - Second way: Send validation errors to js and display them on page.

Upvotes: -1

Related Questions