ASP.Net MVC: Razor-like Helper Methods in ASPX Views

Your MVC views often contain boring repetitive routine that you often get rid of by introducing helper methods within your ascx/aspx or code-behind.
This is what I mean.

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

<script runat="server">
public MvcHtmlString SayHello(string name, string yadayada)
{
    var html = "";
	if(name != null)
		html += string.Format("<div class=\"hello\">{0} {1},</div>", 
			Model.FunMode ? "Howdy" : "Greeting", 
			Html.Encode(name));
	
	html += "<div class=\"label\">I've always wanted to say this:</div>";
	
	foreach (var yada in yadayada.Split(','))
	    html += string.Format("<p class=\"message\">{0}</p>", Html.Encode(yada));

    return MvcHtmlString.Create(html);
}
</script>

<%: SayHello(Model.Chef, "Excellent food!,Taste of heaven,Bye bye!"); %>
<%: SayHello(Model.Mom, "You're the best mommy,I love you,I need money"); %>
<%: SayHello(Model.Enemy, "^@$#!!,##@#$!,%#$ %$ ^#$@$!!!!"); %>

The problem of writing these helper methods, as you can see above, is that you’ll have to leave the very comfort that aspx templating-engine provides, and you’re left dealing with string concatenation, character escaping, and HTML encoding in plain C# code yourself. Not terribly fun.

If you look at the brand new Razor view-engine, it has helper syntax that allows you to write helper methods much more succinctly.

@helper SayHello(string name, string yadayada)
{
    @if(name != null){
		<div class="hello">
			@(Model.FunMode?"Howdy":"Greeting"),
			@Name
		</div>
	}
	
	<div class="label">I've always wanted to say this:</div>
	
	@foreach (var yada in yadayada.Split(',')){
	    <p class="message">@yada</p>
	}
}

@SayHello(Model.Chef, "Excellent food!,Taste of heaven,Bye bye!");
@SayHello(Model.Mom, "You're the best mommy,I love you,I need money");
@SayHello(Model.Enemy, "^@$#!!,##@#$!,%#$ %$ ^#$@$!!!!");

This syntax lets you leverage the full power of your view-engine to template your html contents, and we are jealous.

We are jealous because we’re still using aspx view engine in asp.net mvc2, boo hoo, what can we do about that?

Not a big deal, even since asp.net mvc1, we have actually always been able to write similar razor-ish helper methods right on our aspx view engine. Anonymous delegate is the key.

<% Action<string, string> SayHello = (name, yadayada)=> { %>
	<% if(name != null){ %>
		<div class="hello">
			<%:(Model.FunMode?"Howdy":"Greeting")%>,
			<%:Name%>
		</div>
	<%}%>
	
	<div class="label">I've always wanted to say this:</div>
	
	<% foreach (var yada in yadayada.Split(',')){ %>
	    <p class="message"><%:yada%></p>
	<%}%>
<%}%>

<% SayHello(Model.Chef, "Excellent food!,Taste of heaven,Bye bye!"); %>
<% SayHello(Model.Mom, "You're the best mommy,I love you,I need money"); %>
<% SayHello(Model.Enemy, "^@$#!!,##@#$!,%#$ %$ ^#$@$!!!!"); %>

So there you have it, a helper method on ASP.NET MVC1 that leverages the full comfort of your aspx templating engine.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s