2

I hope this is not a duplicate post.

I have searched internet on how to find out if a model is null in jQuery. There are plenty of examples. I have tried many of them. However, I keep getting problem for this particular issue

Here are the pseudo codes

//class declaration
public class class2
{
    public int var1 { get; set; }
    public int var2 { get; set; }
}

public class class1
{
     //other properties

    public class2 InnerClass { get; set; }

}

//controller
  class1 CLASS1 = GetClass1()
  class1= GetClass1()
  CLASS1.class2 = NULL 



//VIEW

@model class1

//java script
var isObjectNotnull = @(Model.InnerClass != null );
var x;
if (isObjectNotnull )
    x = @(Model.InnerClass.var1);
else
    x = -1;

The error happens in this line

x = @(Model.InnerClass.var1);

I got this error

Object reference not set to an instance of an object.

I thought these two statements will solve the problem

var isObjectNotnull = @(Model.InnerClass != null );
if (isObjectNotnull )

Obvious, it does not help

Then I comment out the assignment

if (isObjectNotnull )
   alert('a');
   @*x = @(Model.InnerClass.var1);*@

Looks like Javascript will render everything. Since Model.InnerClass is null, Javascript cannot continue to render

What is the best approach.

Oh, I need that variable X as part of jSon variable in Ajax variable. Originally, Model.InnerClass.var1 is assigned to jSon variable. Then I got the error, so I added the codes to check for null object, and I still could not resolve this hurdle

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user12345
  • 181
  • 1
  • 2
  • 18
  • When the controller returns the view, you pass the correct model along (that code is not in the controller section)? – Brian Mains Feb 10 '17 at 18:22
  • it is just pseudo code. initialization/retrieve of view model is done controller.. For this particular instance, CLASS1.class2 is set to null because it is not available. That statement is causing the problem in javascript. As I mentioned, I checked the null object, so no run time error. Still there is a rendering issue. – user12345 Feb 10 '17 at 19:06
  • 1
    I don't know why it is marked as duplicate. I went to that URL, and it does not solve my problem. I am not asking about definition of null or not null. My question despite my checking of null, javascript still complains. Miguel Ramirez did go further in checking null. My guess was during the rendering, browser tries to render the that null object. I am not sure. That is the reason I asked that question. What I expect is not entering the if condition since it is null, still that null reference still got reference during rendering – user12345 Feb 10 '17 at 22:31

1 Answers1

4

You have to test that your model object is not null before trying to access its properties.

Like this:

var isObjectNotnull = @(Model != null && Model.InnerClass != null);

But, assuming that the Model.InnerClass is not null, this will render like this:

var isObjectNotnull  = True;

And you will get the following error in javascript because the mayus "T" letter in "True" will make the browser treat it like a variable instead of the true keyword:

Uncaught ReferenceError: True is not defined

So you have to do something like this:

var isObjectNotnull = @(Model != null && Model.InnerClass != null ? "true" : "false");

this way you'll have a boolean value in the isObjectNotnull variable that that would let javascript know if the model is null or not.

UPDATE

I edited your code so it looks like this:

var isObjectNotnull = @((Model != null && Model.InnerClass != null).ToString().ToLower());
var x;
if (isObjectNotnull)
    x = '@(Model.InnerClass != null ? Model.InnerClass.var1 : "")';
else
    x = -1;

That way you get your desired values in both isObjectNotnull and x variables.

A more simple way:

@if (Model != null && Model.InnerClass != null)
{
    @: var x = '@Model.InnerClass.var1';
}
else
{
    @: var x = -1;
}

This way x gets printed in the response with the corresponding value, the output will be var x = 'some json' or var x = -1.

Miguel Ramirez
  • 321
  • 3
  • 9
  • I still get same problem. I eventually find an alternative method. I don't know if it is right approach. Let me see if I can answer my question – user12345 Feb 10 '17 at 22:32
  • I cannot answer my question bc it is marked as duplicated, and it is not. I declared X as mvc razonr variable. I did all the null checking and assignment (if not null) using C# on view. Then on javascript, the json variable is assigned to @x. – user12345 Feb 10 '17 at 22:40
  • 1
    @user12345 i agree, the question should not have been marked as duplicate. you were getting the null reference exception because `Model.InnerClass.var1` was always being evaluated because you were trying to print it. (remember that javascript code is evaluated in the browser client and razor code is evaluated on the server before, all the javascript sorrounding the razor statements are just plain text for the server.). I edited the answer with working examples. – Miguel Ramirez Feb 11 '17 at 17:43
  • thanks. I think that was what I end up doing. Thank you, you are the only that tries to help me. The other people just marked duplicate without looking in detail. I researched a lot, and I could not find the answer. That was the reason I posted. Eventually, I thought outside the box. It works – user12345 Feb 12 '17 at 23:23