问题描述
我已经创建了一个实现引导字符串字段的EditorTemplate如下:
I've created an EditorTemplate for string fields that implements bootstrap as follows:
@using MyProject
@model object
<div class="form-group">
@Html.LabelFor(m => m, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
@Html.TextBox(
"",
ViewData.TemplateInfo.FormattedModelValue,
htmlAttributes)
@Html.ValidationMessageFor(m => m, null, new { @class = "help-block" })
</div>
</div>
和我可以调用这仅仅是这样的:
And I can call this simply like this:
@Html.EditorFor(model => model.FirstName,"BootstrapString")
我的问题:
我怎么会为一个DropDownList做到这一点,这样我可以只叫@ Html.EditorFor如下:
My Question:How would I do this for a DropDownList so that I can merely call @Html.EditorFor as follows:
@Html.EditorFor(model => model.CategoryId,new SelectList(ViewBag.Categories, "ID", "CategoryName"))
所以它基本上是一个通用的DropDownList与Twitter的引导风格。
So it's basically a Generic DropDownList with Twitter Bootstrap styling.
推荐答案
选项1
创建一个 EditorTemplate
名为 BootstrapSelect.cshtml
@model object
<div class="form-group">
@Html.LabelFor(m => m, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
@Html.DropDownListFor(m => m, (SelectList)ViewBag.Items, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m, null, new { @class = "help-block" })
</div>
</div>
和视图
@Html.EditorFor(m => m.CategoryId, "BootstrapSelect")
但是这意味着你送花儿给人需要在控制器指定`ViewBag.Items
but this means you would alway need to assign `ViewBag.Items in the controller
var categories = // get collection from somewhere
ViewBag.Items = new SelectList(categories, "ID", "CategoryName");
选项2
修改 EditorTemplate
来接受额外的ViewData P>
Modify the EditorTemplate
to accept additional ViewData
@model object
<div class="form-group">
@Html.LabelFor(m => m, new { @class = "col-md-3 control-label" })
<div class="col-md-9">
@Html.DropDownListFor(m => m, (SelectList)ViewData["selectList"], new { @class = "form-control"})
@Html.ValidationMessageFor(m => m, null, new { @class = "help-block" })
</div>
</div>
和视图通过的SelectList
在 additionalViewData code>参数
@Html.EditorFor(m => m.CategoryId, "BootstrapSelect", new { selectList = new SelectList(ViewBag.Categories, "ID", "CategoryName") })
这是在你不需要靠ViewBag更好。例如,如果你有一个视图模型与属性公开的SelectList CategoryItems {搞定;组; }
那么你可以使用
this is better in that you don't need to rely on ViewBag. For example, if you had a view model with a property public SelectList CategoryItems { get; set; }
then you could use
@Html.EditorFor(m => m.CategoryId, "BootstrapSelect", Model.CategoryItems)
选项3
创建了自己的助手利用内置的辅助方法。
Create your own helper utilizing the built-in helper methods
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());
}
}
}
和视图
@Html.BootstrapDropDownFor(m => m.CategoryId, new SelectList(ViewBag.Categories, "ID", "CategoryName"))
这篇关于EditorTemplate为DropDownList的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!