7

Is there a good way for creating a tabular display template in ASP.NET MVC3 with the new Razor syntax?

In ASP.NET MVC 2 Phil Haack was blogging about tabular display templates. Is there a better approach in ASP.NET MVC3?

EDIT:

Is it also possible to render the grid on the basis of the model meta data? So I do not want to manually define the columns with the WebGrid API but with model meta data. The problem I see is that you everytime have to define the table by hand using the WebGrid API. I want to have the possibility to have one kind of table that is reusable!

Edit: So is there any good practice for creating grids using model meta data?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rookian
  • 19,841
  • 28
  • 110
  • 180
  • Take a look at Telerik's open source MVC grid, [source](http://telerikaspnetmvc.codeplex.com/), [demo](http://demos.telerik.com/aspnet-mvc/grid). – Kalman Speier Mar 02 '11 at 21:40
  • Is it allowed to use the grid in a commercial non open source project :) ? – Rookian Mar 04 '11 at 19:23
  • 1
    "If you are using Telerik Extensions for ASP.NET MVC Open Source GPL, you are obliged to follow the redistribution rights as stated in the GPL v2.0. If you have purchased a Telerik Extensions for ASP.NET MVC Commercial License you can re-distribute the Telerik product as part of your application without the need to share the source code of your application. View the license terms." http://www.telerik.com/purchase/faqs/aspnet-mvc.aspx – Kalman Speier Mar 04 '11 at 19:58
  • ok so this is no option for me. – Rookian Mar 04 '11 at 23:26

7 Answers7

7

You can use the new WebGrid in MVC3:

@{
    var grid = new WebGrid(Model);
    @grid.GetHtml();
}

And you can use jQuery tabs: http://jqueryui.com/demos/tabs, to create tab pages.

Sorry if I am misunderstood your question.

Kalman Speier
  • 1,937
  • 13
  • 18
1

I haven't used it much but from my understanding you must first setup MVC 3's WebGrid before using it. And you don't want to setup the table in the view but instead do it using model attributes.

If I where to do that I would do the following:

  • Create my custom attributes with which I would decorate my model to define which properties are columns, what labels will be used for column header and anything else you want to configure.

  • I will make a static class with a static method that takes in your model instance, uses reflection to read the properties and your custom attributes and from there it will spit out the WebGrid for you to use in your view.

That's the how but I'll tell you why I wouldn't do this: in MVC you decorate your model for things like validation and that is great and declarative. But when you define a grid, this is a very view specific thing. Sure you may be using the ViewModels which are view specific but I don't think table layout configuration belongs in the model. The way WebGrid or Telerik's Grid before it work is already nicely declarative already.

I short WebGrid and Telerik's Grid are already using best practices by offering declarative, fluent interfaces to define table structure and behaviour. Each table is different so it makes sense you are defining each table. You say "by hand" but this is far from the case as the Grids are doing all the dirty work for you, you are just telling it what you want (declarative programming).

Fabian Nicollier
  • 2,811
  • 1
  • 23
  • 19
  • Using the model meta data seems to have more disadvantages than advantages for me. For instance, using the attribute for displaying text is ugly when you have to support internationalisation (multy languages). – Rookian Mar 16 '11 at 18:22
  • @Rookian: using text attributes for localisation is not ugly. Sadly for pre-MVC 3 you had to specify your own implementation of the data attribute by inheriting from DisplayAttribute and fetching the string from a resource file (I pass resource Type and resource key as parameters). This takes 10 minutes to write once and can be reused. Luckily this was resolved in MVC 3 with support for the DisplayAttribute(resourceKey, resourceType) overload that was ignored due to conflicting dates between MVC 2 release and the model decoration attributes. – Fabian Nicollier Mar 16 '11 at 20:03
0

Just found this answer looking for a similar solution. I was also hoping for something reusable though, so ended up creating a simple template based on Kalman's answer, it looks like this:

View/DisplayTemplates/Table.cshtml

@model IEnumerable<object>
@{WebGrid grid = new WebGrid(Model);}
@grid.GetHtml()

Usage

@Html.DisplayForModel("Table")

This should also work with model meta data for a collection by adding [DataType("Table")] to the property.

Phil
  • 951
  • 12
  • 16
  • 1
    This turned out to be a pretty lame solution. The Webgrid doesn't support data annotations which makes it very limited. I'll leave it here in case it is of use to anyone else though – Phil Jan 22 '13 at 20:00
0

You may take a look at the new WebGrid helper.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I updated my question. WebGrid helper seems to be very ugly in using model meta data :( http://stackoverflow.com/questions/4874544/asp-net-mvc3-webgrid-helper-and-model-metadata – Rookian Mar 04 '11 at 23:27
0

You may also consider using the telerik grid and tabstrip MVC components, check their demos.

Dick Lampard
  • 2,256
  • 1
  • 12
  • 7
0

I like jQuery templates alot. You just need to pass JSON object and it does the rest of the work. good for sorting, pagination etc.

Avi
  • 251
  • 1
  • 4
  • 14
0

Phil Haack wrote a really cool blog article about Templated Razor Delegates. The html helper he writes is simple and light weight.

http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx

BentOnCoding
  • 27,307
  • 14
  • 64
  • 92