Mangesh
Mangesh

Reputation: 3997

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.

Upvotes: 6

Views: 3720

Answers (2)

Jack Spektor
Jack Spektor

Reputation: 1117

You could use DisplayFor. It's another way to show your data.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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).

Upvotes: 4

Related Questions