【问题标题】:Html.SelectFor razor syntax add classHtml.SelectFor 剃刀语法添加类
【发布时间】:2013-09-17 10:22:23
【问题描述】:

我想在 selectfor 中添加一个类

但我得到一个错误

@Html.SelectFor(m => m.foo, optionLabel: null, new { @class = "foo12" })

使用文本框即可:

@Html.TextBoxFormattedFor(m => m.foo, new { @class = "foo" })

我得到的错误:

命名参数规范必须出现在所有固定参数指定之后。

【问题讨论】:

  • 我得到的错误:命名参数规范必须在所有固定参数都指定后出现。

标签: asp.net css razor


【解决方案1】:

错误是不言自明的——任何命名参数(在本例中为“optionLabel”)都必须在未命名参数之后。所以不要这样:

@Html.SelectFor(m => m.foo,      // 1
    optionLabel: null,           // 2
    new { @class = "foo12" }     // 3
)

我猜你可能想要这个:

@Html.SelectFor(m => m.foo,                      // 1
    optionLabel: null,                           // 2
    htmlAttributes: new { @class = "foo12" }     // 3
)

编辑

您的意思是DropDownListFor,而不是“SelectListFor”?您还需要提供选项。像这样的:

@{ 
    var selectList = new SelectListItem[] 
    { 
        new SelectListItem { Text = "text", Value = "value" },
    };
}
@Html.DropDownListFor(m => m.foo, 
    selectlist: selectlist,
    htmlAttributes: new { @class = "foo" }
)

【讨论】:

  • 是的,这行得通,就在我检查回线程之前发现了。谢谢@Html.SelectFor(m => m.foo, optionLabel: null, htmlAttributes: new { @class= "foo" })
猜你喜欢
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多