Rendering pre-formatted HTML in MVC 2 (1 of 2)

I’ve been working more again on this MVC project for my brothers company. As I’m going through some of the changes, one thing was kind of messy that I wanted to fix.

The business rule for "customer" is – if they have a first and last name, show Last, First. If there is also a company, show that smaller, in parenthesis after. If it’s just a company, show that in large text – and if no first, last, or company name is provided, show (No Customer Information). So my example outputs could be:

Smith, John
Smith, John (ABC Company)
ABC Company
(No Customer Information)

The code in the view for that is downright nasty (here I go, fighting with WordPress again – I’m sure this code will be mutilated!!):

<% if (string.IsNullOrWhiteSpace(item.t_Customer.CompanyName))

    {

        if (string.IsNullOrWhiteSpace(item.t_Customer.FirstName) && string.IsNullOrWhiteSpace(item.t_Customer.LastName))

        {%>

            <%: Html.ActionLink("(No Customer Information)", "Details", "Customer")%>

        <%}

        else

        { %>

        <%: Html.ActionLink(item.t_Customer.LastName + ", " + item.t_Customer.FirstName, "Details", "Customer")%>

        <%} %>

<%}

    else

    {

        if (string.IsNullOrWhiteSpace(item.t_Customer.FirstName) && string.IsNullOrWhiteSpace(item.t_Customer.LastName))

        {%>

            <%: Html.ActionLink(item.t_Customer.CompanyName, "Details", "Customer")%>

        <%}

        else

        { %>

        <%: Html.ActionLink(item.t_Customer.LastName + ", " + item.t_Customer.FirstName, "Details", "Customer")%>

        <i>(<%: item.t_Customer.CompanyName %></i>

        <%}

    }%>

No, that isn’t entirely WordPress mutilating the code – that is just nasty code. What’s worse, is since the first/last OR company name need to be bigger, this doesn’t account for that. Whenever you do a <%: that appears to do a Server.HtmlEncode(string)  – which means I can’t include any HTML in the markup.

So, that leaves me with doing manual Response.Writes, like this:

<% if (string.IsNullOrWhiteSpace(item.t_Customer.CompanyName))

    {

        if (string.IsNullOrWhiteSpace(item.t_Customer.FirstName) && string.IsNullOrWhiteSpace(item.t_Customer.LastName))

        {%>

<a href="<%: Url.Action("Details", "Customer")%>" class="customer">(No Customer Information)</a>

<%}

else

{ %>

<a href="<%: Url.Action("Details", "Customer")%>" class="customer">

    <% Response.Write(item.t_Customer.LastName + ", " + item.t_Customer.FirstName);%></a>

<%} %>

<%}

    else

    {

        if (string.IsNullOrWhiteSpace(item.t_Customer.FirstName) && string.IsNullOrWhiteSpace(item.t_Customer.LastName))

        {%>

<a href="<%: Url.Action("Details", "Customer")%>" class="customer">

    <% Response.Write(item.t_Customer.CompanyName);%></a>

<%}

else

{ %>

<a href="<%: Url.Action("Details", "Customer")%>" class="customer">

    <% Response.Write(item.t_Customer.LastName + ", " + item.t_Customer.FirstName);%></a>

<i>(<%: item.t_Customer.CompanyName %></i>

<%}

}%>

<a href="<%: Url.Action("Details", "Customer", new { id=item.CustomerID})%>">

    <% Response.Write(item.t_Customer.FullNameForHtml); %></a>

}

To be clear, this is a chunk of code to JUST display the customer name. This is a trainwreck!! Keep in mind that I have to had this code in many places on many pages. This is beyond ridiculous. So, how do we solve this? It seems there are a few options (from my current level of understanding):

  • Create a public property on my entity class that does this formatting and called it FullNameForHtml, for example.
  • Create a customer HtmlHelper – although that doesn’t seem like a good idea, for something so specific.
  • Put this functionality in a user control, and render it every where I need a customer name.
  • I could also probably use those DisplayTemplates.

Well, I ran out of time tonight – but I was going to try option #1 and #4. With option 1, create a public property, here’s what I did:

public string FullNameForHtml

{

    get

    {

        if (string.IsNullOrWhiteSpace(this.CompanyName))

        {

            if (string.IsNullOrWhiteSpace(this.LastName) && string.IsNullOrWhiteSpace(this.FirstName))

            {

                return "<span class="customer">(No Company or Name Specified: ID#" + this.CustomerID.ToString() + ")</span>";

            }

            else

            {

                return string.Format("<span class="customer">{0}, {1}</span>", this.LastName, this.FirstName);

            }

        }

        else

        {

            if (string.IsNullOrWhiteSpace(this.LastName) && string.IsNullOrWhiteSpace(this.FirstName))

            {

                return "<span class="customer">" + this.CompanyName + "</span>";

            }

            else

            {

                return string.Format("<span class="customer">{1}, {2}</span> <i>({0})</i>", this.CompanyName, this.LastName, this.FirstName);

            }

        }

    }

}

Hey, that doesn’t seem so bad! Then in the .ASPX, I can just do <%: Html.ActionLink(item.t_Customer.FullNameForHtml, "Details"… – but no, sadly – this doesn’t not work. <%: does a Server.HtmlEncode – so all that HTML markup gets rendered as literal strings!

So it would seem I can address this with a DisplayTemplate – I’ll try that tomorrow. Meanwhile, if anyone has run into this, what is a better way to do this? Leave a comment here or shoot me an e-mail! Thanks

Tagged with: , , , ,
Posted in .NET 4.0, ASP.NET, ASP.NET MVC, Entity Framework, Uncategorized
One comment on “Rendering pre-formatted HTML in MVC 2 (1 of 2)
  1. […] HTML in MVC 2 (2 of 2) Leave a Comment Posted by Rob Seder on February 8, 2011 As noted here, I was struggling with how to do somewhat complex formatting – but have that same formatting be […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: