在 ASP.NET MVC 中防止 NullReferenceExceptions 的正确模式是什么

发布于 2024-10-04 05:55:28 字数 1454 浏览 1 评论 0原文

更新 问题是语法问题。 @awrigley 展示了在 Razor 中编写此内容的正确方法。

工作原理如下:

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

您需要将给定 Bar 的前 1 个 Foo 的详细信息显示为 HTML 表格。如果 Foo 为空,如何隐藏空表或显示“未找到”消息?

例如。我在以下行中收到 NullReferenceException,因为 Model.Thing.Propnull

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

它故意为 null,我的存储库返回 null 而不是空的 Foo。但这稍微偏离了重点,即给定一个 null Model.Thing.Prop,我不想调用 Html.RenderPartial

更新
我尝试了以下操作,但没有成功:

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

这导致 Visual Studio 告诉我它在第 1 行第 1 列处期待 ; ,而且 ; 是一个无效的表达式第 1 行,第 1 列(我猜这是由于 MVC3 的预发布状态),如果我在浏览器中点击该页面,我会得到

CS1501:“Write”方法没有重载,需要 0 个参数

并且 @Html.RenderPartial 行突出显示。

我也尝试过

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

,但这会导致我的部分视图中出现 NullReferenceException ,这似乎不正确。 Model.Thing 绝对是一个有效的 Bar 并且 Model.Thing.Prop 绝对是一个 null Foo

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独孤求败 2024-10-11 05:55:28

我想您不想使用...

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

...因为您仍然想渲染部分视图的一部分?

哦,不。已正确阅读您的帖子。

我不知道为什么上面的代码不适合你。

编辑:

请记住,在 Razor 中,@ 可以通过两种方式使用:

  1. 执行语句:

    @{
    我的声明;
    }

  2. 评估 HtmlHelper、返回某种形式或其他形式的字符串(例如,HtmlString、MvcHtmlString 或字符串)的类方法、属性或变量。例如:

    @MyClass.MyStringProperty

请注意,在情况 2 中,不需要终止分号。

1 和 2 表明,如果您有一个返回字符串以外的内容(例如 void)的 htmlhelper,则必须按如下方式调用它:

@{Html.MyHelperThatReturnsVoid;}

而对于返回字符串或 HtmlString 或 MvcHtmlString 的 HtmlHelper,您可以简单地编写:

@Html.MyHelperThatReturnsAString

有关更多详细信息,请参阅我提出的问题的接受的答案

返回 void 问题的自定义 HtmlHelper


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


睫毛上残留的泪 2024-10-11 05:55:28

这要么是 MVC3 的变化,要么是一些奇怪的事情。

MVC2 行为:
如果 Model.Thing 为 null,那么是的,它是 null 引用异常。
如果 Model.Thing 存在且 Thing.Prop 为 null,则您将向模型传递 null 引用。当您将 null 引用传递给模型时,父模型(此处的 Model)将作为模型传递给“SomePartialView”,该模型需要 Prop 类型(除非 Model 和 Prop 是同一类型)。

考虑到您获得 null 引用,我将假设 Model.Thing 为 null。在这种情况下,您唯一的解决方案是 @if 语句。将 null 传递给模型引用根本不起作用,我对这种行为感到有些恼火。

再说一遍,这是 MVC2 行为,如果 MVC3 发生了变化,您可以诚实地传递空模型,太棒了!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文