0

I have this code

   if ($("#ProgramMode").val() == "Edit")
 {

     alert("@ViewBag.ProgramMode");
     @**@obj.dataModel = {
         data: JSON.parse("@Model.ClientDeds".replace(/"/g, '"')),
         location: "remote",
         sorting: "local",
         sortIndx: "ReferralTypeID",
         sortDir: "down"
     };
 }

When the $("#ProgramMode").val() is evaluated to "Edit" it all works good , as "@Model.ClientDeds" is not null.

However when $("#ProgramMode").val() is evaluated to "Add" , the statement :

JSON.parse("@Model.ClientDeds".replace(/"/g, '"'))

Throws a NullReferenceException since "@Model" is null. if I remove the statement

 JSON.parse("@Model.ClientDeds".replace(/"/g, '"'))

and replace it by [] or NUll. it works

This should not be happening,as the if statement is evaluated to false, but it seems that the interpreter checks the value of the elements returned by the server, right? what is the solution?

I checked that the if statement is correctly evaluated in the inspector.

NOTE : I solved it by updating the Create() action method, and change the returning type from :

return view()

to

return view ("Create",new MyObject());

but still need to know why it parse it, inside the if event if it is false

Plexis Plexis
  • 302
  • 2
  • 12
  • Put `@Model.ClientDeds` null checking (with default assignment if `Model` has null value) in another separate statement, then if the parser result exists you can pass it as `JSON.parse` argument. – Tetsuya Yamamoto Jul 20 '18 at 07:43

3 Answers3

1

I guess you remix the razor method with the js method.

$("#ProgramMode").val() seem lik jquery get id='ProgramMode' value, but it can't work in razor.

If you want to check ViewBag.ProgramMode value in razor you need to use @ with if and you might use <text> tag contain your js code.

@{ 
    if (ViewBag.ProgramMode == "Edit")
    {
        <text>
            @**@obj.dataModel = {
                data: JSON.parse("@Model.ClientDeds".replace(/&quot;/g, '"')),
                location: "remote",
                sorting: "local",
                sortIndx: "ReferralTypeID",
                sortDir: "down"
                };
          </text>
    }
} 
D-Shih
  • 44,943
  • 6
  • 31
  • 51
1

MVC razor views (.cshtml files) are processed at server side before they are sent to the browser - during this processing cshtml code is converted to HTML code as the browser do not know what cshtml is - it understands HTML. As part of the server side processing every expression starting with @ sign in your .cshtml file is evaluated on server side. If you are not passing the model to your View, you will get null reference exception - as in this case.

For example, if Model.ClientDeds contain an array of strings like ["John", "Doe", "Willis"], then the line in your section

data: JSON.parse("@Model.ClientDeds".replace(/&quot;/g, '"')),

will be sent as the following line to the browser:

data: JSON.parse("["John", "Doe", "Willis"]".replace(/&quot;/g, '"'))

take a look at this link to understand the role of view engine in this process: What is view engine? What does it actually do?

Farooq Hanif
  • 1,779
  • 1
  • 15
  • 22
0

I solved it by updating the Create() action method, and change the returning type from :

return view()

to

return view ("Create",new MyObject());
Plexis Plexis
  • 302
  • 2
  • 12