admin管理员组文章数量:1312677
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?
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?
Share Improve this question asked Jun 5, 2016 at 13:47 Praveen Kumar VadigePraveen Kumar Vadige 391 gold badge2 silver badges6 bronze badges 05 Answers
Reset to default 4For 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.
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
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>
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
}
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.
本文标签: javascriptJquery to Check ModelState is Valid or notStack Overflow
版权声明:本文标题:javascript - Jquery to Check ModelState is Valid or not - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741907372a2404226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论