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!
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