- Home
- LLBLGen Pro
- Architecture
ASP.NET MVC, TypedLists and Model Meta Data
Joined: 07-Apr-2008
For a new project i want to get started with ASP.NET MVC.
I've been messing around a day or 2 now, trying to determine the best practice, but i'm having a lot of trouble to decide which way to go.
I'm only focussing on displaying in GridViews right now.
Currently i'm using TypedLists, because they are much more lightweight then EntityCollections.
My models all inherit from a PagingSortingModel and look something like this:
public class DebtorGridModel : PagingSortingModel
{
public DebtorGridViewTypedList Debtors { get; set; }
}
...
public abstract class PagingSortingModel
{
public String SortBy { get; set; }
public Boolean SortAscending { get; set; }
public int CurrentPageIndex { get; set; }
public int PageSize { get; set; }
public int TotalRecords { get; set; }
public int PageCount
{
get
{
return this.TotalRecords / this.PageSize + (this.TotalRecords % this.PageSize > 0 ? 1 : 0);
}
}
}
I also have other Models, for Customers, Companies etc. etc.
As you can see, the DebtorGridModel returns a property Debtors, of type DebtorgridViewTypedList. This allows me to create a strongly typed View for DebtorGridViewTypedList like this:
@model MvcApplication1.Models.DebtorGridModel
<table class="grid">
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
....
</tr>
@foreach (var item in Model.Debtors)
{
<tr>
<td class="left">@String.Format("{0:000000}", item.Id)</td>
<td class="left">@item.Name</td>
<td class="left">@item.City</td>
....
}
etc. etc.
Sorting, Paging etc all works fine this way and the designer as complete control over the View without too much hassle.
However...
Few things do not work the way i want:
This is a strongly typed view, but i don't want to make a gridview for every other entitytype collection i have. So i figured i try making a generic view, where the columnheaders are generated based on the Properties in the TypedList. That's where i got stuck.
All the TypedLists derive from TypedListBase<DataRow>, so i thought of making a stronly typed view for the base class. Unfortunately i cannot cast the different TypedLists to that base class, so that's a dead end. I have no idea what other generic types i can use for this purpose.
Furthermore, the TypedLists don't contain MetaData attributes like DisplayName, DataType, Required etc. etc, and i have no idea on how to have them added by LLBLGen Pro. Without these, all properties of a DebtorGridViewRow will be displayed, like RowState, Table, HasErrors etc.
Have i totaly lost it? Anyone else who has been working with MVC and generic gridviews ?
Is mapping all the results from the database to a custom model with properties defined for each field, and Arrays the only other solution to make use of those Model Metadata and create that generic GridView View?
Joined: 29-Sep-2004
kdekok wrote:
For a new project i want to get started with ASP.NET MVC.
I've been messing around a day or 2 now, trying to determine the best practice, but i'm having a lot of trouble to decide which way to go.
I'm only focussing on displaying in GridViews right now.
Currently i'm using TypedLists, because they are much more lightweight then EntityCollections.
My models all inherit from a PagingSortingModel and look something like this:
public class DebtorGridModel : PagingSortingModel { public DebtorGridViewTypedList Debtors { get; set; } } ... public abstract class PagingSortingModel { public String SortBy { get; set; } public Boolean SortAscending { get; set; } public int CurrentPageIndex { get; set; } public int PageSize { get; set; } public int TotalRecords { get; set; } public int PageCount { get { return this.TotalRecords / this.PageSize + (this.TotalRecords % this.PageSize > 0 ? 1 : 0); } } }
I also have other Models, for Customers, Companies etc. etc.
As you can see, the DebtorGridModel returns a property Debtors, of type DebtorgridViewTypedList. This allows me to create a strongly typed View for DebtorGridViewTypedList like this:
@model MvcApplication1.Models.DebtorGridModel <table class="grid"> <tr> <th>ID</th> <th>Name</th> <th>City</th> .... </tr> @foreach (var item in Model.Debtors) { <tr> <td class="left">@String.Format("{0:000000}", item.Id)</td> <td class="left">@item.Name</td> <td class="left">@item.City</td> .... } etc. etc.
Sorting, Paging etc all works fine this way and the designer as complete control over the View without too much hassle.
However...
Few things do not work the way i want:
This is a strongly typed view, but i don't want to make a gridview for every other entitytype collection i have. So i figured i try making a generic view, where the columnheaders are generated based on the Properties in the TypedList. That's where i got stuck.
All the TypedLists derive from TypedListBase<DataRow>, so i thought of making a stronly typed view for the base class. Unfortunately i cannot cast the different TypedLists to that base class, so that's a dead end. I have no idea what other generic types i can use for this purpose.
Furthermore, the TypedLists don't contain MetaData attributes like DisplayName, DataType, Required etc. etc, and i have no idea on how to have them added by LLBLGen Pro. Without these, all properties of a DebtorGridViewRow will be displayed, like RowState, Table, HasErrors etc.
Have i totaly lost it? Anyone else who has been working with MVC and generic gridviews ?
Is mapping all the results from the database to a custom model with properties defined for each field, and Arrays the only other solution to make use of those Model Metadata and create that generic GridView View?
Could you not create a helper that builds the columns based on the data tables columns property? And yes, you are going to lose your data annotations / meta data using TypedLists. How about projections using Linq? Then you have complete control over sprinkling whatever meta you want onto your class.
Joined: 28-Nov-2005
Other options:
a. As Marco said, you can construct the grid based upon the columns, all typedlists/typedviews are datatables.
b. Write your own custom templates that generates those classes. So you wont end up with a generic method but a code for every typedlist. The good news is that it's no complex and you can modify a specific generated class if you want. Everything generic not always is good. Also, in the designer you can put custom properties in the "output settings" section of every typedlist/field. You can use such properties to know how the headers should be named or something like that. See the ASP.Net GUI Templates for example.
c. Just put custom propertis in the "output settings" on your desire typedlists and fields. You can use such properties in your generated code to know the header labels, for instance.