C# 如何将HTMLAttributes传播到自定义编辑器模板?

C# 如何将HTMLAttributes传播到自定义编辑器模板?,c#,asp.net-mvc,razor,mvc-editor-templates,C#,Asp.net Mvc,Razor,Mvc Editor Templates,我为货币浮动格式创建了一个编辑器模板,如下所示 @using System.Globalization @{ var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID); } <div class="input-group "> <div class="input-group-addon">@ri.Curr

我为货币浮动格式创建了一个编辑器模板,如下所示

@using System.Globalization
@{
    var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
}

<div class="input-group ">
    <div class="input-group-addon">@ri.CurrencySymbol</div>
    @Html.TextBox("", ViewData.ModelMetadata.Model, new { @class = " form-control text-box single-line" })
</div>
“文本危险输入lg表单组lg”类未设置为主分区

如何将这些类传播到编辑器模板?

只需将route ViewData[“HtmlatAttributes”]用于字典并获取“class”值即可

@{
var htmlAttrib=ViewData[“htmlAttributes”]
IDictionary dic=新的RouteValueDictionary(htmlAttrib);
var类别=dic[“类别”];
}
你的控制。。。。

您为哪种类型制作了此编辑器?(float,int,string)您可以尝试在模板
@Html.TextBox(“”,ViewData.ModelMetadata.Model,new{@class=“form control text box single line”+(string)ViewData[“ClassText”]
上使用ViewData,如下图所示(model=>model.money,new{ClassText=“text danger input lg form group lg”})@FelipeDeguchi float您需要将属性作为
附加视图数据传递(而不是
HtmlAttributes
)阅读
EditorTemplate
中的
ViewData
,并将值添加到html中。我个人不喜欢将css类传递给模板(EditorFor/DisplayFor)。如果你的模板很复杂,你也会应用哪些HTML节点的值?这是不明确的,如果你更改模板,其他模板/页面称之为该模板,这只是一场噩梦,所以我从不建议这样做。
@Html.EditorFor(model => model.money, new { htmlAttributes = new { @class = "text-danger input-lg form-group-lg" } })
@{
    var htmlAttrib = ViewData["htmlAttributes"]
    IDictionary<string, object> dic = new RouteValueDictionary(htmlAttrib);
    var classes = dic ["class"];
}

<div class="@classes" > your control .... </div>