我应该使用HtmlHelper还是滚动我自己的控件?

时间:2022-10-19 22:04:58

I'm just beginning to learn ASP.NET MVC and I've run into a question. I'm trying to determine whether I should use HtmlHelper to create client controls or if I should just roll my own. My gut wants to lean towards just rolling my own because it gives me total control - and use jQuery to decorate and add cross-browswer functionality. But then I can see advantages of using HtmlHelper for various complex controls that may involve things like paging.

我刚刚开始学习ASP.NET MVC,我遇到了一个问题。我正在尝试确定是否应该使用HtmlHelper来创建客户端控件,或者我是否应该使用自己的控件。我的内心想要倾向于自己滚动,因为它让我完全控制 - 并使用jQuery来装饰和添加交叉浏览器功能。但后来我可以看到使用HtmlHelper进行各种复杂控制的好处,这些控件可能涉及分页等问题。

I'm looking for experiences about when it was better to use HtmlHelper and when it was better to roll your own.

我正在寻找有关何时使用HtmlHelper以及什么时候更好地推出自己的经验。

4 个解决方案

#1


3  

The more they add to HtmlHelper the more I end up using them myself.

他们添加到HtmlHelper越多,我自己就越多地使用它们。

Take a look at these posts from ScottGu: MVC Preview 5 and MVC Beta

看一下ScottGu的这些帖子:MVC Preview 5和MVC Beta

If you don't use the HtmlHelper versions of the input boxes, you won't get the nice, free validation.

如果您不使用输入框的HtmlHelper版本,您将无法获得良好的免费验证。

Also, I'm not sure what prevents you from decorating the HtmlHelper controls with whatever you need to make them usable for jQuery.

另外,我不确定是什么阻止你用你需要的任何东西来装饰HtmlHelper控件以使它们可用于jQuery。

You can add whatever attributes you need using the htmlAttributes overload like so:

您可以使用htmlAttributes重载添加所需的任何属性,如下所示:

<%= Html.TextBox("LastName", ViewData.Model.LastName, new { @class = "required" })%>

The interesting part for what I'm talking about is the anonymous object you see there (new { @class = "required" }).

我所谈论的有趣部分是你在那里看到的匿名对象(新{@class =“required”})。

You can put anything in there you'd need, and it gets slapped onto the attribute section of the control.

你可以在其中放置任何你需要的东西,它会被打到控件的属性部分。

Good luck!

#2


2  

Rolling your own is pretty easy for the easy cases, but it gets harder the more "magical" you want them to be.

对于简单的案例来说,滚动自己很容易,但是你想要它们变得越“神奇”越难。

Luckily you can build the as you go.

幸运的是,你可以随心所欲地建造它。

The provided HtmlHelper controls are pretty extensive, however. They'll provide most of what you'll need. Just be cautious about the overloads, as it's very easy to accidentally use the wrong overload for your calls.

但是,提供的HtmlHelper控件非常广泛。它们将提供您需要的大部分内容。只需要对过载保持谨慎,因为很容易意外地为你的呼叫使用错误的过载。

#3


1  

The real question you need to ask yourself is, do you need total control, do you just need a working control that gets the job done in the fastest time possible (this doesn't mean you have to sacrifice quality).

您需要问自己的真正问题是,您是否需要完全控制,您是否只需要一个能够在最快的时间内完成工作的工作控制(这并不意味着您必须牺牲质量)。

If speed is an issue, use HtmlHelper, if you want to play in control land, then go with rolling your own. If you're not sure, go with HtmlHelper and save homebrewed controls for your spare time experimentation.

如果速度是一个问题,使用HtmlHelper,如果你想在控制区玩,然后去自己滚动。如果您不确定,请使用HtmlHelper并保存自制的控件以供您进行空余时间实验。

#4


0  

I created a fluent interface for HTML, and I have decided to share it here. Maybe you will find it a good alternative. I also wrote a blog post about it.

我为HTML创建了一个流畅的界面,我决定在这里分享它。也许你会发现它是一个很好的选择。我还写了一篇关于它的博客文章。

Here's a teaser:

这是一个预告片:

<%=this.TextBox(x => x.FirstName).Class("required").Label("First Name:")%>
<%=this.CheckBox("enabled").LabelAfter("Enabled").Title("Click to enable.").Styles(vertical_align => "middle")%>

Critique and contributions are welcome.

欢迎批评和贡献。

#1


3  

The more they add to HtmlHelper the more I end up using them myself.

他们添加到HtmlHelper越多,我自己就越多地使用它们。

Take a look at these posts from ScottGu: MVC Preview 5 and MVC Beta

看一下ScottGu的这些帖子:MVC Preview 5和MVC Beta

If you don't use the HtmlHelper versions of the input boxes, you won't get the nice, free validation.

如果您不使用输入框的HtmlHelper版本,您将无法获得良好的免费验证。

Also, I'm not sure what prevents you from decorating the HtmlHelper controls with whatever you need to make them usable for jQuery.

另外,我不确定是什么阻止你用你需要的任何东西来装饰HtmlHelper控件以使它们可用于jQuery。

You can add whatever attributes you need using the htmlAttributes overload like so:

您可以使用htmlAttributes重载添加所需的任何属性,如下所示:

<%= Html.TextBox("LastName", ViewData.Model.LastName, new { @class = "required" })%>

The interesting part for what I'm talking about is the anonymous object you see there (new { @class = "required" }).

我所谈论的有趣部分是你在那里看到的匿名对象(新{@class =“required”})。

You can put anything in there you'd need, and it gets slapped onto the attribute section of the control.

你可以在其中放置任何你需要的东西,它会被打到控件的属性部分。

Good luck!

#2


2  

Rolling your own is pretty easy for the easy cases, but it gets harder the more "magical" you want them to be.

对于简单的案例来说,滚动自己很容易,但是你想要它们变得越“神奇”越难。

Luckily you can build the as you go.

幸运的是,你可以随心所欲地建造它。

The provided HtmlHelper controls are pretty extensive, however. They'll provide most of what you'll need. Just be cautious about the overloads, as it's very easy to accidentally use the wrong overload for your calls.

但是,提供的HtmlHelper控件非常广泛。它们将提供您需要的大部分内容。只需要对过载保持谨慎,因为很容易意外地为你的呼叫使用错误的过载。

#3


1  

The real question you need to ask yourself is, do you need total control, do you just need a working control that gets the job done in the fastest time possible (this doesn't mean you have to sacrifice quality).

您需要问自己的真正问题是,您是否需要完全控制,您是否只需要一个能够在最快的时间内完成工作的工作控制(这并不意味着您必须牺牲质量)。

If speed is an issue, use HtmlHelper, if you want to play in control land, then go with rolling your own. If you're not sure, go with HtmlHelper and save homebrewed controls for your spare time experimentation.

如果速度是一个问题,使用HtmlHelper,如果你想在控制区玩,然后去自己滚动。如果您不确定,请使用HtmlHelper并保存自制的控件以供您进行空余时间实验。

#4


0  

I created a fluent interface for HTML, and I have decided to share it here. Maybe you will find it a good alternative. I also wrote a blog post about it.

我为HTML创建了一个流畅的界面,我决定在这里分享它。也许你会发现它是一个很好的选择。我还写了一篇关于它的博客文章。

Here's a teaser:

这是一个预告片:

<%=this.TextBox(x => x.FirstName).Class("required").Label("First Name:")%>
<%=this.CheckBox("enabled").LabelAfter("Enabled").Title("Click to enable.").Styles(vertical_align => "middle")%>

Critique and contributions are welcome.

欢迎批评和贡献。