gpt4 book ai didi

asp.net - DropDownList 的编辑器模板

转载 作者:行者123 更新时间:2023-12-04 09:06:25 24 4
gpt4 key购买 nike

我为实现 Bootstrap 的字符串字段创建了一个 EditorTemplate,如下所示:

@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>

我可以这样称呼它:
@Html.EditorFor(model => model.FirstName,"BootstrapString")

我的问题:
我将如何为 DropDownList 执行此操作,以便我只能调用@Html.EditorFor,如下所示:
@Html.EditorFor(model => model.CategoryId,new SelectList(ViewBag.Categories, "ID", "CategoryName"))

所以它基本上是一个带有 Twitter Bootstrap 样式的 Generic DropDownList。

最佳答案

选项 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>

并且在 View 中
@Html.EditorFor(m => m.CategoryId, "BootstrapSelect")

但这意味着您总是需要在 Controller 中分配 `ViewBag.Items
var categories = // get collection from somewhere
ViewBag.Items = new SelectList(categories, "ID", "CategoryName");

选项 2

修改 EditorTemplate接受额外的 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>

并在 View 中传递 SelectListadditionalViewData范围
@Html.EditorFor(m => m.CategoryId, "BootstrapSelect", new { selectList = new SelectList(ViewBag.Categories, "ID", "CategoryName") })

这更好,因为您不需要依赖 ViewBag。例如,如果您的 View 模型具有属性 public SelectList CategoryItems { get; set; }那么你可以使用
@Html.EditorFor(m => m.CategoryId, "BootstrapSelect", Model.CategoryItems)

选项 3

使用内置的辅助方法创建您自己的辅助方法
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());
}
}
}

并且在 View 中
@Html.BootstrapDropDownFor(m => m.CategoryId, new SelectList(ViewBag.Categories, "ID", "CategoryName"))

关于asp.net - DropDownList 的编辑器模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com