关于asp.net:DropDownList的EditorTemplate

EditorTemplate for DropDownList

我为实现自举的字符串字段创建了EditorTemplate,如下所示:

1
2
3
4
5
6
7
8
9
10
@using MyProject
@model object

    @Html.LabelFor(m => m, new { @class ="col-md-3 control-label" })
   
        @Html.TextBox(
           "",
            ViewData.TemplateInfo.FormattedModelValue,
            htmlAttributes)
        @Html.ValidationMessageFor(m => m, null, new { @class ="help-block" })

我可以这样简单地称呼它:

1
@Html.EditorFor(model => model.FirstName,"BootstrapString")

我的问题:
我将如何为DropDownList执行此操作,以便仅按如下方式调用@ Html.EditorFor:

1
@Html.EditorFor(model => model.CategoryId,new SelectList(ViewBag.Categories,"ID","CategoryName"))

因此,它基本上是带有Twitter Bootstrap样式的通用DropDownList。


选项1

创建一个名为BootstrapSelect.cshtmlEditorTemplate

1
2
3
4
5
6
@model object

  @Html.LabelFor(m => m, new { @class ="col-md-3 control-label" })
 
    @Html.DropDownListFor(m => m, (SelectList)ViewBag.Items, new { @class ="form-control"})
    @Html.ValidationMessageFor(m => m, null, new { @class ="help-block" })

并认为

1
@Html.EditorFor(m => m.CategoryId,"BootstrapSelect")

但这意味着您总是需要在控制器中分配`ViewBag.Items

1
2
var categories = // get collection from somewhere
ViewBag.Items = new SelectList(categories,"ID","CategoryName");

选项2

修改EditorTemplate以接受其他ViewData

1
2
3
4
5
6
@model object

  @Html.LabelFor(m => m, new { @class ="col-md-3 control-label" })
 
    @Html.DropDownListFor(m => m, (SelectList)ViewData["selectList"], new { @class ="form-control"})
    @Html.ValidationMessageFor(m => m, null, new { @class ="help-block" })

并在视图中传递additionalViewData参数中的SelectList

1
@Html.EditorFor(m => m.CategoryId,"BootstrapSelect", new { selectList = new SelectList(ViewBag.Categories,"ID","CategoryName") })

这样更好,因为您不必依赖ViewBag。 例如,如果您的视图模型具有属性public SelectList CategoryItems { get; set; },则可以使用

1
@Html.EditorFor(m => m.CategoryId,"BootstrapSelect", Model.CategoryItems)

选项3

利用内置的帮助程序方法创建自己的帮助程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace YourAssembly.Html
{
  public static class BootstrapHelper
  {
    public static MvcHtmlString BootstrapDropDownFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, SelectList selectList)
    {      
      MvcHtmlString label = LabelExtensions.LabelFor(helper, expression, new { @class ="col-md-3 control-label" });
      MvcHtmlString select = SelectExtensions.DropDownListFor(helper, expression, selectList, new { @class ="form-control" });
      MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper, expression, null, new { @class ="help-block" });
      StringBuilder innerHtml = new StringBuilder();
      innerHtml.Append(select);
      innerHtml.Append(validation);
      TagBuilder innerDiv = new TagBuilder("div");
      innerDiv.AddCssClass("col-md-9");
      innerDiv.InnerHtml = innerHtml.ToString();
      StringBuilder outerHtml = new StringBuilder();
      outerHtml.Append(label);
      outerHtml.Append(innerDiv.ToString());
      TagBuilder outerDiv = new TagBuilder("div");
      outerDiv.AddCssClass("form-group");
      outerDiv.InnerHtml = outerHtml.ToString();
      return MvcHtmlString.Create(outerDiv.ToString());
    }
  }
}

并认为

1
@Html.BootstrapDropDownFor(m => m.CategoryId, new SelectList(ViewBag.Categories,"ID","CategoryName"))