ASP.NET MVC 3: Razor View Engine Support Added!

ASP.NET Team Blog
21 January 2011

Update: Watch the webinar video that shows DevExpress with ASP.NET MVC 3 Razor View Engine.

I’m excited to announce that all DevExpress ASP.NET MVC Extensions support ASP.NET MVC 3 and it’s new Razor View Engine!

Feel free to use any DevExpress ASP.NET MVC Extension with the MVC 3 release.

How to use the Razor view engine with DevExpress MVC Extensions

Start using the DevExpress MVC Extensions in the Razor view engine. Let me show you how…

All you need to do is to tweak the script and style registration code for the Razor view engine:

1. Register scripts and styles

Here I’ve added the scripts and style registration statements below the HEAD tag in the Shared/_Layout.cshtml file:

@using DevExpress.Web.Mvc.UI

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>@ViewBag.Title </title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    @Html.DevExpress().GetStyleSheets(
        new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new StyleSheet { ExtensionSuite = ExtensionSuite.Editors },
        new StyleSheet { ExtensionSuite = ExtensionSuite.HtmlEditor },
        new StyleSheet { ExtensionSuite = ExtensionSuite.GridView }
    )
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.4.4.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>
    @Html.DevExpress().GetScripts(
        new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new Script { ExtensionSuite = ExtensionSuite.HtmlEditor },
        new Script { ExtensionSuite = ExtensionSuite.GridView },
        new Script { ExtensionSuite = ExtensionSuite.Editors }
    )
</head>

<body>

Now use any DevExpress MVC extension in the view. Note: Please be sure that if you do declare the scripts and styles in the Master Page (Shared/_Layout.cshtml file) then you either derive your view from there or declare them in the new view directly.

2. Two Changes for Razor

a. To render HTML code, use the GetHtml() rather than Render() method. The following code shows how to accomplish this for the GridView extension:

@Html.DevExpress().GridView( settings => {
     ...
}).Bind(Model).GetHtml()

b. To write a string followed by a line terminator to the response, use the DevExpressHelper.WriteLineToResponse method and pass the string as its parameter. The code below shows how to customize a pager template for the GridView extension:

@Html.DevExpress().GridView( settings => {
    MVCxGridView grid = c.NamingContainer as MVCxGridView;
    ...
    settings.SetPagerBarTemplateContent(c => {
        ...
        DevExpressHelper.WriteLineToResponse("<label>of " + @grid.PageCount + "</label>");
        ...
    });
}).Bind(Model).GetHtml()

That’s it!

Online Example w/Download

Want to see an online working example that you can also download immediately and run in your local Visual Studio? Check out this Code Central example:

How to use the Razor view engine with DevExpress Extensions for ASP.NET MVC

Learn how to use Code Central here.

Project Template Coming Soon

A new Visual Studio 2010 project template will be released soon to help you create new ASP.NET MVC 3 projects for DevExpress MVC Extensions. The project template will be very similar to our current MVC project template. Track this suggestion if you want to know when the exact date it’ll be released.

You don’t need the project template to use DevExpress MVC Extensions in the Razor view engine. You can start now.

ASP.NET MVC 3

Last week, Scott Guthrie announced the release of ASP.NET MVC 3 (and some other fun bits). The third major release of ASP.NET MVC 3 brings some great features.

You can download and install ASP.NET MVC 3 here.

What is Razor?

It’s a new view engine for ASP.NET MVC:

Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow.

Unlike most template syntaxes, with Razor you do not need to interrupt your coding to explicitly denote the start and end of server blocks within your HTML. The Razor parser is smart enough to infer this from your code. This enables a compact and expressive syntax which is clean, fast and fun to type.  - Scott Guthrie, Microsoft

ASP.NET MVC Commitment

The ASP.NET MVC 3 support is part of our commitment to ASP.NET MVC. For 2011, we’re adding more features and extensions to the DevExpress ASP.NET MVC Extensions. Join my webinar to learn more.

 

Try the DevExpress ASP.NET MVC Extensions now with ASP.NET MVC 3, then drop me a line below with your thoughts.

Thanks!

Follow MehulHarry on Twitter

Build Your Best - Without Limits or Compromise

Try the DevExpress ASP.NET MVC Extensions online now: http://mvc.devexpress.com

Read the latest news about DevExpress ASP.NET MVC Extensions

Download a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.