Wednesday, 5 August 2009

ASP.NET Dynamic Data - How to add a ReadOnly attribute to a Property

Problem:
I have the following property on my entity framework designer that I want to make it ReadOnly:

[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int FilterParameterId
{
get
{
return this._FilterParameterId;
}
set
{
// etc
}
}
Solution:
1.
Make two copies of the Text.ascx field template and name one ReadOnlyText.ascx (is used on edit page and list) and the other ReadOnlyText_Edit.ascx (is used on Insert page) then annotate the field as follows

2.
[MetadataType(typeof(FilterParameter_MD))]
public partial class FilterParameter
{
}
3.
public class FilterParameter_MD
{
[UIHint("ReadOnlyText")]
public object FilterParameterId { get; set; }
}

No comments: