I am using the Html.DisplayFor() and Html.EditorFor() helper methods to auto-scaffold a Display view and Edit view for a Municipality entity.
My Application layer is used to retrieve a Municipality class:
public class Municipality
{
public int ID { get; set; }
public string Name { get; set; }
public int ProvinceID { get; set; }
public string ProvinceName { get; set; }
}
For the Display view this entity is mapped to a DisplayMunicipality class containing Display meta data:
[MetadataType(typeof(Metadata))]
public class DisplayMunicipality : Municipality
{
private class Metadata
{
[ScaffoldColumn(false)]
public int ID { get; set; }
[ScaffoldColumn(false)]
public int ProvinceID { get; set; }
[DisplayName("Province")]
public string ProvinceName { get; set; }
}
public DisplayMunicipality() { }
public DisplayMunicipality(Municipality municipality)
{
this.ID = municipality.ID;
this.Name = municipality.Name;
this.ProvinceName = municipality.ProvinceName;
}
}
For the Edit view this entity is mapped to an EditMunicipality class containing Edit meta data:
[MetadataType(typeof(Metadata))]
public class EditMunicipality : Municipality
{
private class Metadata
{
[ScaffoldColumn(false)]
public int ID { get; set; }
[Required()]
[StringLength(50)]
public string Name { get; set; }
[DisplayName("Province")]
[Required()]
[UIHint("ProvinceDropDown")]
public int ProvinceID { get; set; }
[ScaffoldColumn(false)]
public string ProvinceName { get; set; }
}
public EditMunicipality() { }
public EditMunicipality(Municipality municipality)
{
this.ID = municipality.ID;
this.Name = municipality.Name;
this.ProvinceID = municipality.ProvinceID;
}
}
This works fine but it requires a lot of extra code, not only the code mentioned above but also all kinds of conversions inside my controller.
Is it possible to put the meta data in the Municipality class that is returned from the Application layer? In that case I need some mechanism to specify that a property should be shown in the Display view or Edit view.
Michel Metse...
Member
5 Points
4 Posts
Get rid of all the extra code to use auto-scaffolding and meta data
Apr 16, 2010 07:13 AM|LINK
I am using the Html.DisplayFor() and Html.EditorFor() helper methods to auto-scaffold a Display view and Edit view for a Municipality entity.
My Application layer is used to retrieve a Municipality class:
public class Municipality { public int ID { get; set; } public string Name { get; set; } public int ProvinceID { get; set; } public string ProvinceName { get; set; } }For the Display view this entity is mapped to a DisplayMunicipality class containing Display meta data:
[MetadataType(typeof(Metadata))] public class DisplayMunicipality : Municipality { private class Metadata { [ScaffoldColumn(false)] public int ID { get; set; } [ScaffoldColumn(false)] public int ProvinceID { get; set; } [DisplayName("Province")] public string ProvinceName { get; set; } } public DisplayMunicipality() { } public DisplayMunicipality(Municipality municipality) { this.ID = municipality.ID; this.Name = municipality.Name; this.ProvinceName = municipality.ProvinceName; } }For the Edit view this entity is mapped to an EditMunicipality class containing Edit meta data:
[MetadataType(typeof(Metadata))] public class EditMunicipality : Municipality { private class Metadata { [ScaffoldColumn(false)] public int ID { get; set; } [Required()] [StringLength(50)] public string Name { get; set; } [DisplayName("Province")] [Required()] [UIHint("ProvinceDropDown")] public int ProvinceID { get; set; } [ScaffoldColumn(false)] public string ProvinceName { get; set; } } public EditMunicipality() { } public EditMunicipality(Municipality municipality) { this.ID = municipality.ID; this.Name = municipality.Name; this.ProvinceID = municipality.ProvinceID; } }This works fine but it requires a lot of extra code, not only the code mentioned above but also all kinds of conversions inside my controller.
Is it possible to put the meta data in the Municipality class that is returned from the Application layer? In that case I need some mechanism to specify that a property should be shown in the Display view or Edit view.
Thanks in advance!
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: Get rid of all the extra code to use auto-scaffolding and meta data
Apr 16, 2010 10:48 AM|LINK
I think that the only solution is to make an attribute of your own and modify the MVC code...