【问题标题】:@Html.RadioButtonFor: Invalid anonymous type member declarator [duplicate]@Html.RadioButtonFor:无效的匿名类型成员声明符 [重复]
【发布时间】:2017-09-07 02:50:19
【问题描述】:

我正在尝试在单选按钮选择上显示一个弹出窗口。

我使用 MVC 剃须刀。

以下是我的代码

 @Html.RadioButtonFor(model => Model.Container.Type, Model.container.type.1,
                       new { id = "type1", @class = "validate[required]", data-toggle = "modal", data-target = "#Modal" })

我收到以下错误

CS0746:无效的匿名类型成员声明符。匿名类型成员必须用成员赋值、简单名称或成员访问权限声明

似乎属性 data-toggle 和 data-target 不适用于 @Html.RadioButtonFor()

【问题讨论】:

  • @Html.RadioButtonFor(model => model.Container.Type, ...(小写m)但是Model.container.type.1是什么?并且必须是data_toggle(在匿名对象上使用时下划线)
  • HTML 助手中正确的属性用法是data_toggle = "modal", data_target = "#Modal"。为什么它绑定到Model.Container.Type 而表达式包含m binder(应该是m => m.Container.Type 或类似的东西)?

标签: c# asp.net-mvc razor


【解决方案1】:
@Html.RadioButtonFor(model => Model.Container.Type, Model.container.type.1, new { id = "type1", @class = "validate[required]", dataToggle = "modal", dataTarget = "#Modal" })

连字符不能用于变量名。您可以改用驼峰式表示法

如果您别无选择,只能使用连字符,请使用下划线。它将自动替换为连字符。

【讨论】:

  • 它不再是使用dataToggle = "modal"data- 属性(只是一个无效属性),camel-case 毫无意义,因为它将被转换为小写
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多