7
Answers

asp.net core verify a model is not null before jquery

Photo of Marius Vasile

Marius Vasile

3y
1.1k
1

I have a query to be executed on a btn click. The problem is that I can't even open the page because the model is null which I know it is innitialy but becomes not null based on a parameter passed later. What I tried is

$(function(){
                $("#btnVer").on("click", function(){
                    if("@(Model.IntrebareClass != null)")
                    {
                        //checkA
                        if($("#rasA").hasClass("colorA") && checkA == "@Model.IntrebareClass.CorectA")
                        {
                            $("#rasA").css("background-color", "green")
                        }
JavaScript

this is a part of the jquery. How do I fix this?

Answers (7)

3
Photo of Marius Vasile
573 2k 162.7k 3y
I manage to fix it by using a JSON call
 
  1. $.getJSON(`?handler=CorrectDetails&id=${id}`, (data) => {  
  2.                     if (!jQuery.isEmptyObject(data)) {  
  3.   
  4.                         //checkA  
  5.                         if($("#rasA").hasClass("colorA") && checkA == data.corectA)  
  6.                         {  
  7.                             $("#rasA").css("background-color""green");  
  8.                         }  
  9.                         else if($("#rasA").hasClass("colorA") && checkA != data.corectA)  
  10.                         {  
  11.                             $("#rasA").css("background-color""red");  
  12.                         }  
  13.                         else if($("#rasA").hasClass("colorB") && checkA == data.corectA)  
  14.                         {  
  15.                             $("#rasA").css("background-color""gray");  
  16.                         }  
 
3
Photo of Satya Karki
9 56.7k 3.6m 3y
Hi, you can try this 
  1. if ($("#YourFormName").valid()) {  
  2. //checkA  
  3.                         if($("#rasA").hasClass("colorA") && checkA == "@Model.IntrebareClass.CorectA")  
  4.                         {  
  5.                             $("#rasA").css("background-color""green")  
  6.                         }  
  7. }  
 To validate from Controller, you can use ModelState
  1. if (!ModelState.IsValid) return BadRequest("Enter required fields");  
 
3
Photo of Sachin Singh
NA 55.8k 87.9k 3y
test this
  1. $(function(){  
  2.                 $("#btnVer").on("click", function(){  
  3.                 var cond="@Model.IntrebareClass";  
  4.                     if(cond!=' ')  
  5.                     {  
  6.                         //checkA  
  7.                         if($("#rasA").hasClass("colorA") && checkA == "@Model.IntrebareClass.CorectA")  
  8.                         {  
  9.                             $("#rasA").css("background-color""green")  
  10.                         }  
 
2
Photo of Sachin Singh
NA 55.8k 87.9k 3y
what if you check with boolean
  1. if($("#rasA").hasClass("colorA") && checkA == Boolean("@Model.IntrebareClass.CorectA") )   
 
2
Photo of Marius Vasile
573 2k 162.7k 3y
Also, the error is on line 
 
  1. if($("#rasA").hasClass("colorA") && checkA == "@Model.IntrebareClass.CorectA")   
 
 checkA was assigned true value with 
 
  1. var checkA == true;  
and Model.Intrebare.CorectA is the result of a checkbox, true or false 
2
Photo of Marius Vasile
573 2k 162.7k 3y
Thank you all for answers, I am using Asp.Net Core Razor pages, my jquery is in the View. I tried both solutions, Satya and Sachin but is not working. The page is loading perfectly without this function but is not working because of the model is null.
I don't see how to use model validation Srinivasan, I am not having issues in code behind 
2
Photo of Srinivasan Ramamoorthi
458 3k 52.7k 3y
Use modelstate.isvalid Check this link https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0