System.NullReferenceException创建一个详细信息查看站点模型,该模型来自区域模型

发布于 2025-02-13 15:53:52 字数 514 浏览 0 评论 0原文

详细信息。cshtml(用于站点)

这是导致问题的代码的一部分,

<b>Region: </b>
<a asp-controller="Regions" asp-action="Details" asp-route-id="@Model.RegionID">
@Model.Region.Name
</a>
</p>

我的网站模型具有一个名为regionID的字段,该字段代表该站点所在的区域,当我试图检索区域名称以在详细信息中显示该区域以显示它网站的视图我正在获取系统。NullReferenceException

关于如何解决此问题的任何想法将不胜感激

Details.cshtml (For Site)

This is the part of the code causing problem

<b>Region: </b>
<a asp-controller="Regions" asp-action="Details" asp-route-id="@Model.RegionID">
@Model.Region.Name
</a>
</p>

my site model has a field called RegionID which represents the region in which the site is located and when i am trying to retrieve the Region name to display it in the details view of the site i am getting the System.NullReferenceException

Any Idea on how i can solve this issue would be appreciatedenter image description here

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

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

发布评论

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

评论(2

朱染 2025-02-20 15:53:52

您必须在详细信息中修复EF代码

var model= await context...
.Include(r=> r.Region)
...

you have to fix you ef code in Details action

var model= await context...
.Include(r=> r.Region)
...
羞稚 2025-02-20 15:53:52

您可以尝试为model.gregion设置默认值:

public class YourModel{
    public int RegionID { get; set; }
    public Region Region { get; set; } = new Region();
}
public class Region {
    public string Name { get; set; }
}

You can try to set a default value for Model.Region:

public class YourModel{
    public int RegionID { get; set; }
    public Region Region { get; set; } = new Region();
}
public class Region {
    public string Name { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文