Jquery to Check ModelState is Valid or not

橙三吉。 提交于 2021-02-08 07:29:33

问题


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?


回答1:


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.




回答2:


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




回答3:


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>



回答4:


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                   
}



回答5:


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.



来源:https://stackoverflow.com/questions/37642629/jquery-to-check-modelstate-is-valid-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!