Monday, June 11, 2012

Display Label for your model in View

Let's see what was annoying in old fashion aspx pages before and how MVC helps us to overcome it.
Consider you want to show the "Product Name" as a label in some pages in aspx pages , there was different possibilities to achieve it , best way to create a usercoontrol for the product and add a label "Product Name" yes it it , but what if you want to show product name in other places as well like another control or another page, yes you need to write it again, this is a simple example but when it comes into a complex project maintaining this simple thing could be a nightmare.

I remember once in our company somebody mistakenly name a label "Blah blah" just for testing and that code mistakenly went into the production, but If we defined these labels in our model level, if you name something wrong you can easily change it in one place and there is more possibilities to catch it sooner at least not on production 


Model----------
public class MyModel
{
    [DisplayName("Product Name")]
    public string ItemName{ get; set; }
}

 View-----------
This is so basic and interesting of using Model metadata.

No comments:

Post a Comment

Thank you for sharing your knowledge and experiences with this weblog.