-1

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

Yiyi You
  • 16,875
  • 1
  • 10
  • 22
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – derpirscher Jul 05 '22 at 12:43

2 Answers2

0

you have to fix you ef code in Details action

var model= await context...
.Include(r=> r.Region)
...
Serge
  • 40,935
  • 4
  • 18
  • 45
0

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; }
}
Yiyi You
  • 16,875
  • 1
  • 10
  • 22