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:- [sourcecode language=“csharp”]
[/sourcecode]
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:- [sourcecode language=“csharp”] @Html.Raw(Html.Pager((IPagination)Model).ToString()) [/sourcecode]
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:- [sourcecode language=“csharp”] 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(); } [/sourcecode]
With Razor syntax this changes to return MvcHtmlString as below:- [sourcecode language=“csharp”] 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()); } [/sourcecode]
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.