可能的MVC属性空引用

huangapple go评论57阅读模式
英文:

Possibly Null Reference on MVC Property

问题

在我的ASP.NET Core 7 MVC代码中,我有一个视图模型,其中有一个可选的Address属性:

public class CustomerViewModel
{
    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public Address? Address { get; set; }
}

当我在ASP.NET Core MVC中尝试使用它时,出现了警告

CS8602:可能为null引用的取消引用

视图:

<th scope="col">@Html.DisplayNameFor(x => x.First().Address.State)</th>

如何推荐调用此属性而不产生编译警告?

谢谢。

英文:

Actually in my ASP.NET Core 7 MVC code, I have a view model that takes an optional Address property:

public class CustomerViewModel
{
    public Guid Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public Address? Address { get; set; }
}

When I try to use it within ASP.NET Core MVC, I get the warning

> CS8602: Dereference of a possibly null reference

View:

&lt;th scope=&quot;col&quot;&gt;@Html.DisplayNameFor(x =&gt; x.First().Address.State)&lt;/th&gt;

What is the recommended way to call this property without any compiler warning?

Thanks.

答案1

得分: 1

Using the ! (null-forgiving) operator:

@Html.DisplayNameFor(x => x.First().Address!.State)

Check Microsoft sample on GitHub.

英文:

Using the ! (null-forgiving) operator:

@Html.DisplayNameFor(x =&gt; x.First().Address!.State)

Check Microsoft sample on GitHub.

答案2

得分: 1

Here's the translated text without the code parts:

When I try to use it within ASP.NET Core MVC, I get the warning

Actually, this is expected behavior based on your possible nullable definition on Address? and the compiler is expecting there might be a chance of null occurrence, which the compiler warns about as follows:

Thus, the compiler wants to set a nullability check before the very statement.

What is the recommended way to call this property without any compiler warning?

As per our official guideline, you can simply get rid of your warnings by putting a null check ! after Address property like: First().Address!.State which other contributors have already shown you.

英文:

> When I try to use it within ASP.NET Core MVC, I get the warning

Actually, this is expected behavior based on your possible nullable defination on Address? and compiler is expecting there might be a change of null occurrence which compiler warning as following:

可能的MVC属性空引用

Thus, compiler wanting to set a nullability check before the very statement.

> What is the recommended way to call this property without any compiler
> warning?

As per our official guideline, you can simply get rid of your warnings by putting a null checking ! after Address property like: First().Address!.State which other contributors already shown you.

&lt;th scope=&quot;col&quot;&gt;@Html.DisplayNameFor(x =&gt; x.First().Address!.State)&lt;/th&gt;

可能的MVC属性空引用

可能的MVC属性空引用

Note: I am trying to enhance the answer because we have particular document for CS8602 warning code which is Dereference of a possibly null reference and my sole purpose is to refer you correct document.

答案3

得分: 0

<th scope="col">
@{
var address = Model.First().Address;
if (address != null)
{
@Html.DisplayNameFor(_ => address.State)
}
}
</th>

英文:

You will need to explicitly check the first returned element for null

&lt;th scope=&quot;col&quot;&gt;
@{
    var address = Model.First().Address;
    if (address != null)
    {
        @Html.DisplayNameFor(_ =&gt; address.State)
    }
}
&lt;/th&gt;

huangapple
  • 本文由 发表于 2023年3月31日 03:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定