I'm not sure how to best explain this so I have linked to a picture that shows the problem. I have a simple table that I render and for each td item it calls DisplayForModel() so that I can render different things inside the table.
The problem I am having is when a database record has null values. For example in this screenshot the Address, Phone and Fax fields. DisplayForModel appears to be rendering all the properties of the view model.
To give a bit of context, I pull out some records in my controller and select them into an object[]. The reason I use an object array rather than strings etc. is so that I can define templates for different object types.
public ActionResult List(int? page, int? departmentId, string search)
{
var model = (from e in UserRepository.FindAllUsersThatMatch(CompanyId, departmentId.GetValueOrDefault(-1), search).IsNotDeleted()
select new object[] {
new Link() { Name = string.Format("{0} {1}", e.Firstname, e.Surname), LinkAddress = Url.Action("Edit", "Employee", new { Id = e.Id} ) } ,
e.DateOfBirth,
e.StreetAddress,
e.Suburb ?? "",
e.State,
e.Postcode,
e.Home,
e.Mobile,
e.EmailAddress
}).ToDynamicListView(page, Request.RequestContext.RouteData, "Employee", "DOB", "Street Address", "Suburb", "State", "Postcode", "Home", "Mobile", "Email");
return View("List", model);
}
The ToDynamicListView html helper setups up my view model I have called DynamicListModel The rows of this model is a paged list like this;
DynamicListModel model = ...
model.Rows = new PagedList<DynamicListRow>(source, page.Value, 10);
In my view I will pass this model off to a partial for render (that renders the table of data you see in the linked image.
<% Html.RenderPartial("DynamicListView", Model); %>
This creates the html for the table and does something similar to
<table>
<thead>
<tr><th>header1</th><th>header2</th> etc.</tr>
</thead>
foreach (row)
{
<tr>
foreach (cell)
{
<td><% Html.RenderPartial("DynamicListCell", cell); %></td>
}
} </tr>
</table>
So each cell is rendered using DisplayForModel. The partial is quite simple and looks like this;
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<object>" %>
<%= Html.DisplayForModel()%>
I have tried checking if the object passed into the partial is null and not rendering but that doesn't seem to work either.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<object>" %>
<% if (Model != null) {%>
<%= Html.DisplayForModel()%>
<% } %>
The reason I am using ViewUserControl rather than a proper typed partial is because this DisplayForPartial is being called on each cell inside the list of rows.
This was mentioned as each td cell of the table further up. This allows me to render a different template depending on the type.