views:

70

answers:

2

I would like to avoid to specify manually maxlength attributes for all form input elements, and instead to use size information from the Data Model if possible:

e.g.

<%= Html.TextBox("Titel", ViewData.Model.Titel, (object)new { @maxlength = "10" })%>

would it be possible "translate" the DbType Attribute in the LINQ class ?

[Column(Storage="_Titel", DbType="NVarChar(10)")]
public string Titel
A: 

Yeah, attributes are available through reflection:

http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx#vcwlkattributestutorialanchor3

friism
A: 

Hi Oliver

Let me get you going onto this course of action and hopefully you can find the rest of the way. I've encapsulated the data select into a LINQ statement and hopefully you can swap it out for however you need the data:

var q = from myDatabaseRow in dbContext.TableName
                    select new
                    {
                        myDatabaseRow.FieldName,
                      (ColumnAttribute)TypeDescriptor.GetProperties(myDatabaseRow.GetType())["FieldName"].Attributes[typeof(ColumnAttribute)]

                    };

What this does is select a property from a table, and then select a new anonymous type. The second property in the anonymous type is the ColumnAttribute you see in the data model field. You can then use the .DbType property to find out the max length. Hope this helps!

Ray Booysen
Thanks for the tip, but unfortuntley I could not make it work, it looks like the cast is wrong:Cannot convert type 'System.ComponentModel.PropertyDescriptorCollection' to 'System.Data.Linq.Mapping.ColumnAttribute'
Oliver
Looks like a misplaced ( or ) in my code.
Ray Booysen