I'm struggling to find real world value in annotating my view models with DisplayAttribute
. The only advantage I can see is that this weakly typed view code:
<label for="IPAddress">IP address</label>
Becomes strongly typed:
@Html.LabelFor(m => m.IPAddress)
The disadvantage is that I need to recompile and redeploy my web app to fix a simple typo.
Wouldn't it be better to keep display-only information where it belongs, in the view, and have some sort of HTML helper to render the correct field ID, like this:
<label for="@Html.IdFor(m => m.IPAddress)">IP address</label>
I guess I'm just curious as to why the MVC guys went for the attribute-based solution. Perhaps I'm missing something obvi开发者_如何学JAVAous?
I believe DisplayAttributes support localization. This could be a good reason not to hard-code English labels...
This blog post may be of interest: Localize ASPNET MVC 2 DataAnnontations
If you only use your entities in a simple web app supporting one language I see no reason to use LabelFor
.
精彩评论