Ral*_*lle 13 css3 asp.net-mvc-3 twitter-bootstrap
我正在将我的应用程序升级到Bootstrap 2.1.1,我需要为许多表单标记添加一个类属性(class ="form-horizontal").问题是,大多数形式使用相当漂亮
@using (Html.BeginForm()) {
Run Code Online (Sandbox Code Playgroud)
我宁愿保留的格式.获取class属性的明显方法是使用以下重载,例如:
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal" })) {
Run Code Online (Sandbox Code Playgroud)
但我宁愿不这样做.向表单添加类属性的最佳方法是什么?
我已经考虑过将.addClass("form-horizontal")的jQuery文档就绪处理程序用于表单,但我担心用户会看到可怕的'无格式内容的闪存'.
另一种方法可能是使用less -style mix-in,将.form-horizontal类添加到所有表单样式规则中,但我还没有少用,所以我不确定这个.
有谁知道解决这个问题的最佳方法?
Ral*_*lle 21
根据StanK的建议,以及关于SO的其他一些答案的建议,我创建了一个专门针对Bootstrap表单布局的扩展方法:
public static MvcForm BeginHorizontalForm(this HtmlHelper helper) {
return helper.BeginForm(null, null, FormMethod.Post, new { @class = "form-horizontal" });
}
Run Code Online (Sandbox Code Playgroud)
这让我可以使用
@using (Html.BeginHorizontalForm()) {...
Run Code Online (Sandbox Code Playgroud)
这是美好而优雅的.
Sta*_*anK 15
如何编写一个调用所需重载的自定义辅助方法,传入'form-horizontal'?
然后,您将使用视图@using (Html.BootstrapBeginForm()) {
,让您的视图干净整洁.