Simple html vs extension methods in Razor (prefere

2019-06-20 07:15发布

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.

2条回答
趁早两清
2楼-- · 2019-06-20 07:31

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

查看更多
戒情不戒烟
3楼-- · 2019-06-20 07:42

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

查看更多
登录 后发表回答