question

BrentHumber-6530 avatar image
0 Votes"
BrentHumber-6530 asked ·

System.NullReferenceException: 'Object reference not set to an instance of an object.'

This error got me, I'm using code first: System.NullReferenceException: 'Object reference not set to an instance of an object.' el IEnumerable<BrentStat5.Context.CorvetteCost> @{ ViewData["Title"] = "Index"; } <h1>Index</h1> <p> <a asp-action="Create">Create New</a> </p> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.CorvetteItem) </th> <th> @Html.DisplayNameFor(model => model.CorvetteItemCost) </th> <th> @Html.DisplayNameFor(model => model.CorvetteStore) </th> <th> @Html.DisplayNameFor(model => model.Repair) </th> <th> @Html.DisplayNameFor(model => model.Upgrade) </th> <th> @Html.DisplayNameFor(model => model.Date) </th> <th> @Html.DisplayNameFor(model => model.Maintance) </th> <th> @Html.DisplayNameFor(model => model.OrginalCost) </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { //error is on this line. <tr> <td> @Html.DisplayFor(modelItem => item.CorvetteItem) </td> <td> @Html.DisplayFor(modelItem => item.CorvetteItemCost) </td> <td> @Html.DisplayFor(modelItem => item.CorvetteStore) </td> <td> @Html.DisplayFor(modelItem => item.Repair) </td> <td> @Html.DisplayFor(modelItem => item.Upgrade) </td> <td> @Html.DisplayFor(modelItem => item.Date) </td> <td> @Html.DisplayFor(modelItem => item.Maintance) </td> <td> @Html.DisplayFor(modelItem => item.OrginalCost) </td> <td> <a asp-action="Edit" asp-route-id="@item.CorvetteId">Edit</a> | <a asp-action="Details" asp-route-id="@item.CorvetteId">Details</a> | <a asp-action="Delete" asp-route-id="@item.CorvetteId">Delete</a> </td> </tr> } </tbody> </table>

dotnet-runtime-core
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please use the code block button, copy code directly into the code block without HTML formatting.
77925-codeblock.png


0 Votes 0 ·
codeblock.png (8.1 KiB)

1 Answer

JerryCai-MSFT avatar image
0 Votes"
JerryCai-MSFT answered ·

Hi,BrentHumber

This error often means model can't get the value from controller, you should pass the value to view:

 public IActionResult Index()
         {
             var model= new List<MyModel>()
             {
                 new MyModel{ CorvetteId=1,CorvetteItem="aa"},
                 new MyModel{ CorvetteId=2,CorvetteItem="bb"}
             };
             return View(model);
         }

Best Regards,
Jerry Cai


If the answer is helpful, please click "Accept Answer" and upvote it.
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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.