asp.net – DropDownList的EditorTemplate

前端之家收集整理的这篇文章主要介绍了asp.net – DropDownList的EditorTemplate前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我为字符串字段创建了一个EditorTemplate,它实现了bootstrap,如下所示:
@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

创建一个名为BootstrapSelect.cshtml的EditorTemplate

@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,new { @class = "help-block" })      
  </div>
</div>

并在视图中

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

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

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

选项2

修改EditorTemplate以接受其他ViewData

@model object
<div class="form-group">
  @Html.LabelFor(m => m,(SelectList)ViewData["selectList"],new { @class = "help-block" })      
  </div>
</div>

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

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

这是更好的,因为您不需要依赖ViewBag.例如,如果您有一个带有属性的视图模型public SelectList CategoryItems {get;组;然后你可以使用

@Html.EditorFor(m => m.CategoryId,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,selectList,new { @class = "form-control" });
      MvcHtmlString validation = ValidationExtensions.ValidationMessageFor(helper,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,"CategoryName"))
原文链接:https://www.f2er.com/aspnet/248259.html

猜你在找的asp.Net相关文章