Reputation: 135
I'm trying to make an edit in a modal.
@foreach (var item in Model.DataList)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.ID)</td>
<td>@Html.DisplayFor(modelItem => item.ProjectName)</td>
<td>@Html.DisplayFor(modelItem => item.Attachment.Name)</td>
<td><a href="#" onclick="editData(@item.ID,'EditModal')" class="btn btn-primary">Edit</a></td>
<input type="hidden" value="@item.ID | @item.ProjectName | @item.Material.MaterialName " id="@item.ID" />
</tr>
}
And this is working without @item.Material.MaterialName
. With this model I got this error:
Error: Object reference not set to an instance of an object in @Html.DisplayFor(modelItem => item.Attachment.Name)
Can anyone tell me why and how to fix it?
I tried:
<td>
@if (item.Attachment != null)
{
@Html.DisplayFor(modelItem => item.Attachment.FileName)
}
</td>
and:
<input type="hidden" value="@item.ID | @item.ProjectName | @item.Material.MaterialName | @item.Attachment.Name " id="@item.ID" />
Upvotes: 1
Views: 166
Reputation: 28
I saw your comment at "And this is working without @item.Material.MaterialName" above line indicate that your model which you bind it from action method having material as null value so you are trying to access NUll values data & which is going to give you error which you mentioned below to that line. try using following things wherever you want to use the properties which might be null @item.Material?.MaterialName & also make sure you are declaring properties are nullable in parent class .
Upvotes: 1