英文:
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:
<th scope="col">@Html.DisplayNameFor(x => x.First().Address.State)</th>
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 => 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:
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.
<th scope="col">@Html.DisplayNameFor(x => x.First().Address!.State)</th>
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
<th scope="col">
@{
var address = Model.First().Address;
if (address != null)
{
@Html.DisplayNameFor(_ => address.State)
}
}
</th>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论