Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 928675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:02:57+00:00 2026-05-15T20:02:57+00:00

I’m not sure how to best explain this so I have linked to a

  • 0

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 http://img844.imageshack.us/img844/4142/displayformodelnullvalu.png

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:02:58+00:00Added an answer on May 15, 2026 at 8:02 pm
    <%@ 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.