I have this code in my index.cshtml. I have a loader indicator that i want to show on this page before the view that displays the table i load with data from a sql procedure. The problem is the loader is displaying if there is a delay in showing the view
with the data, as a result of an error in the ModelState. My question is how do I check if ModelState is valid in the this Javascript code before showing the Loader....
@section Scripts { <script> $("#form-id").on("submit", function ()
{ $("#divLoading").show(); });
</script>
}before I display the view with the table with sql data. Thanks !!!!
i assume you mean check validation, so the loading doesn't show when post back does not happen. it appears you are using unobstrusive valuation, which uses jquery validation, so you just check if valid:
<script>
$("#form-id").on("submit", function () {
if ($("#form-id").valid()) $("#divLoading").show();
});
</script>
Check whether the two files 'jquery.validate.js' and
'jquery.validate.unobtrusive.js' are already referenced on your page.
Second, you just need to modify your code according to @ bruce's solution.
Here is the result.
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
282 Points
298 Posts
Check ModelState in Javascript code
May 07, 2020 06:09 PM|rkrex|LINK
I have this code in my index.cshtml. I have a loader indicator that i want to show on this page before the view that displays the table i load with data from a sql procedure. The problem is the loader is displaying if there is a delay in showing the view with the data, as a result of an error in the ModelState. My question is how do I check if ModelState is valid in the this Javascript code before showing the Loader....
@section Scripts { <script> $("#form-id").on("submit", function ()
{ $("#divLoading").show(); });
</script>
}before I display the view with the table with sql data. Thanks !!!!
All-Star
53041 Points
23626 Posts
Re: Check ModelState in Javascript code
May 07, 2020 06:25 PM|mgebhard|LINK
ModelState is a server construct not JavaScript. Validate the inputs in JavaScript/jQuery or whatever validation framework you are using.
All-Star
58204 Points
15661 Posts
Re: Check ModelState in Javascript code
May 07, 2020 07:37 PM|bruce (sqlwork.com)|LINK
i assume you mean check validation, so the loading doesn't show when post back does not happen. it appears you are using unobstrusive valuation, which uses jquery validation, so you just check if valid:
Contributor
2750 Points
781 Posts
Re: Check ModelState in Javascript code
May 08, 2020 07:11 AM|YihuiSun|LINK
Hi, rkrex
According to your needs, I suggest you to check the following points.
First, check whether you are using client validate correctly.
Second, you just need to modify your code according to @ bruce's solution.
Here is the result.
Best Regards,
YihuiSun