0

I would like to create an HtmlHelper extension method that allows you to set the model instance and then bind properties of this model to render some html.

This would be following a similiar pattern close to the Telerik Grid.

In the following example, we see that we set the Model as the Grid's DataContext. Then declare some columns. Foreach column, they bind a property in respect the the Model which is IEnumerable. So when the grid renders rows, the respective property is bound from the model's instance for that row.

@(Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(o => o.OrderID).Width(100);
            columns.Bound(o => o.ContactName).Width(200);
            columns.Bound(o => o.ShipAddress);
            columns.Bound(o => o.OrderDate).Format("{0:MM/dd/yyyy}").Width(120);
        })
        .Scrollable(scrolling => scrolling.Enabled((bool)ViewData["scrolling"]))
        .Sortable(sorting => sorting.Enabled((bool)ViewData["sorting"]))
)

Basically, I would like ideas on how to implement the Bound method. For my scenario, the Model will also be of IEnumerable. So lets say I have a Model that is List<MyObj>

For argument sake, MyObj looks something like this:

public class MyObj
{
    public bool IsRegistered { get; set;}
    public string Name { get; set; }
}

I would like to end up in the view calling an htmlhelper that would render my list and look something like this:

@(Html.MyHelpers().MyList(Model)
                  .ControlOne.Bound(o => o.IsRegistered)
                  .ControlTwo.Bound(o => o.Name)
)

I haven't tried a whole lot yet and to be honest I'm not sure how to implement this. So I will be posting updates as this will be a work in progress. But, if there are any ideas or suggestions, I would appreciate it. I know I could use the grid, but this helper will be lighter weight for I don't need very much functionality and don't want to drag around the telerik grid for this purpose.

Thanks!

Gabe
  • 49,577
  • 28
  • 142
  • 181

2 Answers2

1

http://publicvoidlife.blogspot.com/2011/02/htmlhelper-extensions.html this post might help you to write strongly typed HtmlHelper extensions.

Relevant Question:

Is it possible to create a custom ASP.NET MVC strongly typed HTML Helper?

And you might want to check out some opensource projects like mvccontrib and djme

Community
  • 1
  • 1
adt
  • 4,320
  • 5
  • 35
  • 54
0

Have you had a look at the source code for Html.DisplayForModel() (since ASP.NET MVC is open source)? Maybe it will give you some clues.

Source code for ASP.NET MVC 3 RTM is here: http://aspnet.codeplex.com/releases/view/58781

The bits you need to look at are DisplayExtensions.cs and TemplateHelpers.cs in mvc3-rtm-sources\mvc3\src\SystemWebMvc\Mvc\Html

The point is that you can already use Html.DisplayForModel() to render a control for each property of your model and you can see how it's done by looking at the source code, so you could easily tweak it.

There's also EditorExtensions.cs which contains Html.EditorForModel() which gives you the edit version.

The important thing about ASP.NET MVC is that you can swap out any part of it and write your own, so you can achieve pretty much anything.

Tom Chantler
  • 14,753
  • 4
  • 48
  • 53