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" "">
<html xmlns="">

    <title>@ViewBag.Title </title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        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>
        new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new Script { ExtensionSuite = ExtensionSuite.HtmlEditor },
        new Script { ExtensionSuite = ExtensionSuite.GridView },
        new Script { ExtensionSuite = ExtensionSuite.Editors }


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 => {

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>");

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.


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.


Follow MehulHarry on Twitter

Build Your Best - Without Limits or Compromise

Try the DevExpress ASP.NET MVC Extensions online now:

Read the latest news about DevExpress ASP.NET MVC Extensions

Download a fully-functional version of DXperience for free now:

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.
Alex Boston
Alex Boston

It's good to see my fav controls are up with the new things.

22 January 2011
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)

Thanks Monzer!

22 January 2011
shashank agnihotri
shashank agnihotri

DevExpress ASP.NET is very good!

24 January 2011
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)

Thanks Shashank!

24 January 2011

Not all your customers use C# - when will you show MVC samples in

26 January 2011
Johnny K
John K

i am sorry they dont have sample at least not all of them

for example

where is the code?

this c# mania is really annoying (its not better if not worst) but when we are talking about fast development vb is first and not the { }

i am expecting every single example to be in both languages for a professional company like your.


27 January 2011

@Mehul ... I don't see any sample on HtmlEditor in .... but hey - that's not the first time you guys prioritize C# developers. Sadly it's enough to give me gray hair and start looking at [other slns], which is far more easy for me to implement than trying to translate from a language I don't know.

27 January 2011
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)

Mojo and Johnny K,

Yes, you're right that we don't have VB.NET on the demo site. Actually, we initially did try but had some issues with VS2008. We support MVC2 still which also works on VS2008.

We can add VB.NET code for the our demos. I've created a suggestion for you. I recommend tracking it to get updates:

Suggestion: VB.NET Demos for MVC

Thanks for the feedback :)

27 January 2011
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)


Which demo particular demo do you need help with right away? Please email me at mharry [at] devexpress and I'll help you convert it.


27 January 2011
Graham Charles
Graham Charles

Hey there!

The code segment on this page (it's inside a pre element, class "xml") is displaying weird on my browser (Chrome Dev build 10.0.642.2, Win 7) -- the ExtensionSuite property name is duplicated on every line, once in red and once in blue, almost as if they're being viewed through a source code version comparison:


new StyleSheet { ExtensionSuiteExtensionSuite = ...

With one "ExtensionSuite" red and the other blue. Very weird.

Anyway, you might want to look at whatever your source code formatting tool is (some javascript magic, no doubt?).


28 January 2011
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)


Thanks for the heads up. We're using a popular code display tool. While we are using a slightly older version of it, it does work fine in most browsers.

I suspect that it's the Chrome Dev Build. That's one of the reasons I switched to the stable Chrome builds. Smile


28 January 2011
Khyati Dixit
Khyati Dixit

I am trying to do following in Razor.. but cudnt figure out how can i add those html.actionlinks in there



       settings =>


           settings.Name = "gvEditing";

           settings.KeyFieldName = "ProductID";

           settings.CallbackRouteValues = new { Controller = "GridView", Action = "EditingPartial" };

           settings.Width = Unit.Percentage(100);

           var commandColumn = settings.Columns.Add("", "#");

           commandColumn.SetDataItemTemplateContent(c =>


               <%= Html.ActionLink("Edit", "EditingEdit", new { ProductID = DataBinder.Eval(c.DataItem, "ProductID") })%> 

               <%= Html.ActionLink("Delete", "EditingDelete", new { ProductID = DataBinder.Eval(c.DataItem, "ProductID") },

                       new { onclick = "return confirm('Do you really want to delete this record?')" })%>


           commandColumn.SetHeaderTemplateContent(c =>


               <%= Html.ActionLink("New", "EditingEdit", new { ProductID = -1 })%>


           commandColumn.Settings.AllowDragDrop = DefaultBoolean.False;

           commandColumn.Settings.AllowSort = DefaultBoolean.False;

           commandColumn.Width = 70;


           var column = settings.Columns.Add("CategoryID", "Category", MVCxGridViewColumnType.ComboBox);

           var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;

           comboBoxProperties.DataSource = ((GridViewController)ViewContext.Controller).GetCategories();

           comboBoxProperties.TextField = "CategoryName";

           comboBoxProperties.ValueField = "CategoryID";

           comboBoxProperties.ValueType = typeof(int);


           settings.Columns.Add("UnitPrice").PropertiesEdit.DisplayFormatString = "c";


           settings.Columns.Add("Discontinued", MVCxGridViewColumnType.CheckBox);

           settings.ClientLayout = (s, e) =>


               if(e.LayoutMode == ClientLayoutMode.Loading)

                   e.LayoutData = (string)Session["GridState"];


                   Session["GridState"] = e.LayoutData;


           settings.PreRender = (s, e) => {

               if(ViewData["VisibleID"] == null) return;

               ASPxGridView grid = (ASPxGridView)s;







28 January 2011
Parrish Guido
Parrish Guido

Good stuff...this post saved us some time figuring out how to use the new @ Razor syntax versus the

19 May 2011
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)

Thanks Parrish and glad it was helpful!

23 May 2011

Please login or register to post comments.