Razor中的简单html vs扩展方法(首选项) [英] Simple html vs extension methods in Razor (preference)

查看:81
本文介绍了Razor中的简单html vs扩展方法(首选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于MVC Razor中的简单"标签,您更喜欢使用简单的HTML还是使用扩展方法 例如

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

我觉得有时候使用简单的HTML更容易. 扩展方法在您要执行一些自定义代码时很有意义.

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

推荐答案

取决于.如果您要与此标签关联的male元素是使用html helper呈现的,例如:@Html.TextBoxFor(x => x.Male),那么我将使用Html.LabelFor并保留lambda和强类型.我也永远不会使用:

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)

,然后用[DisplayName]属性装饰视图模型属性,以便可以控制视图模型上的消息:

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; }

因此,有许多不同的可能方案.有时我也可以只使用静态HTML而没有帮助程序(当前无法想到这种情况,但是我确定它存在).

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

这篇关于Razor中的简单html vs扩展方法(首选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆