Hi @Aleem Ahmed ,
You can refer the following screenshot, since in the HttpGet method, we didn't create a Movie instance to the returned model, in the view page, when we display the movie name it will show the null reference exception.
You can check your HttpGet method, make sure the return data contains the Movie value.
Besides, you can also check the Movie is null when display the Movie. Add a ?
at the end of the Movie
, code like this:
@foreach (var item in Model.ShoppingCart.ShoppingCartItems) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
@item.Movie?.MovieName
</td>
<td>
@Html.DisplayFor(modelItem => item.ShoppingCartId)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
In this scenario, when the Movie is null, it will not throw the Null reference exception, the result like this:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion
yes, it gets returned as null in this view, despite storing values in it previously.