Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple html vs extension methods in Razor (preference)

For Simple tags in MVC Razor do you prefer to use simple HTML or use extension methods eg

<label for="male">Male</label> 
Or   
@Html.Label("male", "Male")

I feel it is sometime more easier to use the simple HTML. Extension methods make sense when you want to do some custom code.

like image 501
Mangesh Pimpalkar Avatar asked Jul 11 '11 18:07

Mangesh Pimpalkar


1 Answers

Depends. If the male element you are associating this label to is rendered with a html helper like: @Html.TextBoxFor(x => x.Male) then I would use a Html.LabelFor and keep the lambdas and strong typing. Also I would never use:

@Html.Label("male", "Male")

I would use a view model and a strongly typed version:

@Html.LabelFor(x => x.Male)

and I would decorate my view model property with the [DisplayName] attribute so that I can control message on my view model:

[DisplayName("foo bar")]
public string Male { get; set; }

So there are like many different possible scenarios. I could also sometimes simply use static HTML and no helpers (currently can't think of such scenario but I am sure it exists).

like image 105
Darin Dimitrov Avatar answered Sep 21 '22 10:09

Darin Dimitrov