tags:

views:

61

answers:

1

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.

alt text

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.

A: 
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<object>" %> 

You should replace the with the name of your viewmodel for starters. So if you have a viewmodel named Person it would be:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %> 

Now at least your partail view will be strongly typed to your view model and not just a generic object. You could also check the values of the view model in the constructor and then set them to empty strings if they are null.

EDIT

If all you want to do is not display anything for null values for could just add the check here:

<table>
<thead>
    <tr><th>header1</th><th>header2</th> etc.</tr>
</thead>
foreach (row)
{
  <tr>
      foreach (cell)
      {
        <td><% if (cell != null) ? Html.RenderPartial("DynamicListCell", cell) : "&nbsp;" %></td>
      }
} </tr>

That will achieve the same result as what you were trying to do by adding a null check around the DisplayForModel.

amurra
I have included extra information in my question as to why I am using ViewUserControl<object>Initialising the viewmodel properties to empty strings would work, but would require me to go through and do it for every model (which I have quite a few).I was hoping there would be a way to change how this is rendered so I only need to do it once.
Joshua Hayes
@Joshua: Can you post more code so we know what your code is doing (like the templates, controller, etc). Also try this blog post to see if it helps any: http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx
amurra
Hi amurra, I have updated the question with more code to show how the table is produced and briefly what happens from the controller through to the view and rendering of each cell in the table. Hope that helps.
Joshua Hayes
Nobody? I would have thought just by looking at the screenshot somebody would know why DisplayFormModel produces that for a null value
Joshua Hayes
Can you supply the rest of the code for the ToDynamicListView helper and the DynamicListModel view model?
amurra
Amurra, that edit of yours did it. Sooo simple. As you can see from my original question, I tried this inside the partial but it didn't work. It didn't even occur to me to do it outside the partial. I knew this had to be easy. Cheers.
Joshua Hayes