Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderSection set as required:false, but still throws "Object reference not set to an instance of an object"

This code supposed to mark my Section as optional.

_layout.cshtml 
@RenderSection("ViewStyles",false)

or

@RenderSection("ViewStyles",required:false)

I tried both.

Yet, it throws an exception

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I tried adding an empty section to my views that don't need to use the ViewStyles section

@section ViewStyles {  }

but I still get the same exception.

Is there another workaround?

like image 726
Roman Mik Avatar asked Nov 19 '14 17:11

Roman Mik


People also ask

What is RenderSection in MVC?

@RenderSection is used for injecting content in the defined section. It allows you to specify a region in Layout. Two steps are there to define @RenderSection in ASP.NET MVC. A. Specify a @RenderSection Area in Layout Page.

How do I call RenderBody?

The call to RenderBody() will be replaced by <div>Some View</div> . Then, RenderBody() will be the same, but the call RenderSection("scripts", ...) will be replaced by <script src="jsfile. js"></script> .

What is the use of RenderBody ()?

RenderBody is used for rendering the content of the child view. In layout pages, it renders the portion of a content page. It takes the content of the child page and merges into the layout.


2 Answers

@Chris-Pratt led me in the right direction with his detailed answer. I wanted to share my code to help out:

My issue was @RenderSection("Header", false) coming back “Object reference not set to an instance of an object”. Stepping throw the code, it would not step pass and break:

header section

Finally, I realized it was the next line of code that breaking:

<body class="@TempData[AppConstants.GlobalClass].ToString() 
         loggedin-@(User.Identity.Name.ToString().ToLower())">

@TempData was null

like image 114
JoshYates1980 Avatar answered Nov 15 '22 08:11

JoshYates1980


This has nothing to do with your section. I'm not sure where you got the idea that the section being empty is generating this error, but that is categorically not what's happening. Object reference not set to an instance of an object is a runtime error generated when you attempt to reference a property off of a variable that evaluates to null. There's some piece of code somewhere that is referencing a property off a variable (again, not talking about sections here) when that variable itself resolves to null at runtime.

For example, lets say you do something like:

Foo foo = db.Foos.Find(id);

The variable foo is defined as a Foo, so you can reference any property off of it that Foo has. If your Foo class had a property named Bar. Then you might then try to get the value of this property somewhere in your code via:

foo.Bar

That will compile just fine. However, if no Foo with the id is found, then the actual value of foo is null, and null does not have a property named Bar, which can only be determined at runtime. That is what the error is telling you is happening: somewhere in your code, you're calling a property of some variable without checking for a null value of the variable first. In the example above, you would typically do something like:

Foo foo = db.Foos.Find(id);
if (foo != null)
{
    bar = foo.Bar;
}

You can also employ a ternary to provide some sort of fallback:

bar = foo != null ? foo.Bar : "Baz";

That way, bar will either hold the value of foo.Bar or if foo is null, the string "Baz".

like image 34
Chris Pratt Avatar answered Nov 15 '22 06:11

Chris Pratt