Simple html vs extension methods in Razor (preference)

For simple tags in MVC Razor, you prefer to use plain HTML or use extension methods like

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

I feel that someday it's easier to use plain HTML. Extension methods make sense if you want to make some kind of custom code.

+6
source share
2 answers

It depends. If the male element associated with this shortcut is displayed using an html helper, for example: @Html.TextBoxFor(x => x.Male) , then I would use Html.LabelFor and save 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 the message in my view model:

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

Thus, there are many different possible scenarios. Sometimes I could just use static HTML and no helpers (currently I can’t come up with such a script, but I'm sure it exists).

+4
source

You can use DisplayFor. This is another way to show your data.

0
source

Source: https://habr.com/ru/post/892517/


All Articles