开发者

What is the correct pattern to protect against NullReferenceExceptions in ASP.NET MVC

开发者 https://www.devze.com 2023-01-27 04:00 出处:网络
UPDATE The issue was a syntax issue. @awrigley shows the correct way to write this in Razor. The following works:

UPDATE The issue was a syntax issue. @awrigley shows the correct way to write this in Razor.

The following works:

@if(Model.Thing.Prop != null)
{
    Html.RenderPartial("SomePartialView", Model.Thing.Prop);
}

You have a requirement to show details for the top 1 Foo for a given Bar as an HTML table. How do you hide the empty table or show a "none found" message if that Foo is null?

For example. I'm getting a NullReferenceException on the following line because Model.Thing.Prop is null;

@{Html.RenderPartial("SomePartialView", Model.Thing.Prop);} 

It is intentionally null, my Repository returns null instead of a empty Foo. But this is mildly beside the point, which is that given a null Model.Thing.Prop, I don'开发者_开发技巧t want to call the Html.RenderPartial.

Update

I've tried the following with no luck:

@if(Model.Thing.Prop != null)
{
    @{Html.RenderPartial("SomePartialView", Model.Thing.Prop);}
}

Which results in Visual Studio telling me it's expecting a ; at line 1, column 1 and also that ; is an Invalid expression at line 1, column 1 (I'm guessing this is due to the pre-release status of MVC3), and if I hit the page in a browser I get

CS1501: No overload for method 'Write' takes 0 arguments

with the @Html.RenderPartial line highlighted.

I also tried

@if(Model.Thing.Prop != null)
{
    <text>
    @{Html.RenderPartial("SomePartialView", Model.Thing.Prop);}
    </text>
}

but this results in a NullReferenceException from within my Partial View, which doesn't seem right. Model.Thing is definitely a valid Bar and Model.Thing.Prop is definitely a null Foo.


I presume you don't want to use...

@if (Model.Thing.Prop != null)
{Html.RenderPartial("SomePartialView", Model.Thing.Prop);} 

...because you still want to render part of the partial view?

Oh, no. Have read your post properly.

I have no idea why the above code doesn't work for you.

EDIT:

Remember that in Razor, @ can be used two ways:

  1. To execute statements:

    @{ MyStatement; }

  2. To evaluate an HtmlHelper, a class method, property or variable that returns a string of some form or other (eg, HtmlString, MvcHtmlString or string). For example:

    @MyClass.MyStringProperty

Note that in case 2, no terminating semi colon is required.

1 and 2 indicate that if you have an htmlhelper that returns something other than a string (eg, void), then you have to call it as follows:

@{Html.MyHelperThatReturnsVoid;}

Whereas with an HtmlHelper that returns a string or HtmlString or MvcHtmlString, you can simply write:

@Html.MyHelperThatReturnsAString

For more detail, see the accepted answer to a question I asked:

Custom HtmlHelper that returns void problem



This is either a change in MVC3 or something odd.

MVC2 Behavior:
If Model.Thing is null, then yes it is a null ref exception.
If Model.Thing exists and Thing.Prop is null, then you'll pass a null reference to model. When you pass a null ref to model, parent model (the Model here) gets passed as the model to "SomePartialView" which would be expecting type Prop (unless Model and Prop are the same type).

Considering you're getting null ref, I'm going to assume that Model.Thing is null. In that case your only solution would be an @if statement. Passing null into a model ref simply doesn't work and I've torn some hair out over the behavior.

Again, this is MVC2 behavior, if this has change for MVC3 where you honestly can pass a null model, awesome!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号