Converting MVC2 app to MVC3 (webform to Razor syntax)

I have been upgrading an MVC 2 web application (which used the web form view) to an MVC 3 (razor syntax) application and have noted a few things I came across and if this can help anyone then thats always nice. I havent went into the dll changes and any web.config changes required to update the app from MVC 2 to MVC 3 – this is more a post about syntax changes I ran into.

The old code looked like this:-

<%= Html.Pager((IPagination)Model)%>

with razor you may see the html being displayed rather than the correct code, to fix this we need to use the Html.Raw() helper which takes a string as follows:-

@Html.Raw(Html.Pager((IPagination)Model).ToString())

Another issue I ran into was that Razor automatically encodes all HTMl and this means that to get my HtmlHelpers working I had t make a very minor change. Thw webform syntax would have been as follows:-

public static string DatePicker(this HtmlHelper helper, string name)
{
    return helper.DatePicker(name, null);
}

public static string DatePicker(this HtmlHelper helper, string name, string imageUrl)
{
    return helper.DatePicker(name, imageUrl, null);
}

public static string DatePicker(this HtmlHelper helper, string name, object date)
{
   return helper.DatePicker(name, "/Content/Images/calendar.gif", date);
}

public static string DatePicker(this HtmlHelper helper, string name, string imageUrl, object date)
{
    var html = new StringBuilder();
    ....
    ....
    ....
    return html.ToString();
}

With Razor syntax this changes to return MvcHtmlString as below:-

public static MvcHtmlString DatePicker(this HtmlHelper helper, string name)
{
    return helper.DatePicker(name, null);
}

public static MvcHtmlString DatePicker(this HtmlHelper helper, string name, string imageUrl)
{
    return helper.DatePicker(name, imageUrl, null);
}

public static MvcHtmlString DatePicker(this HtmlHelper helper, string name, object date)
{
    return helper.DatePicker(name, "/Content/Images/calendar.gif", date);
}

public static MvcHtmlString DatePicker(this HtmlHelper helper, string name, string imageUrl, object date)
{
    var html = new StringBuilder();
    ....
    ....
    ....
    return MvcHtmlString.Create(html.ToString());
}

Hopefully this may come in handy for someone who comes across these items that require changing – I’ll post more if I come across them.

One thought on “Converting MVC2 app to MVC3 (webform to Razor syntax)

  1. This is very interesting, You’re a very professional blogger. I’ve joined your rss feed and sit up for seeking more of your fantastic post. Also, I’ve shared your web site in my social networks!

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