2009-01-25 67 views
0

我想避免为所有表单输入 元素指定手动maxlength属性,而是在可能的情况下使用数据模型中的大小信息:任何方式从LINQ生成的类成员获取大小属性?

例如,

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

是否有可能“翻译”LINQ类中的DbType属性?

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

回答

0

让我让你去到这一行动的,希望你能找到剩下的路。我已经封装在数据选择到一个LINQ声明,希望可以换出来了,但是你需要的数据:

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

        }; 

这样做什么是从表中选择属性,然后选择一个新的匿名类型。匿名类型中的第二个属性是您在数据模型字段中看到的ColumnAttribute。然后,您可以使用.DbType属性找出最大长度。希望这可以帮助!

+0

谢谢你的提示,但unfortuntley我不能让它工作,它看起来像演员是错误的: 无法将类型“System.ComponentModel.PropertyDescriptorCollection”到“System.Data.Linq.Mapping.ColumnAttribute” – Oliver 2009-01-27 20:11:09