【问题标题】:razor view @Html.RadioButtonFor剃刀视图@Html.RadioButtonFor
【发布时间】:2019-09-02 10:12:00
【问题描述】:

我有以下 Razor 代码:

@{
    foreach(var item in ModelBasket.AvailableOptions)
    {
      if (item.Enabled)
      {

      }
    }
}

每个项目都有一个 Selected 属性和 description 属性。 我想为每个项目创建一个单选按钮,但如果我尝试在我的 if 语句中执行以下操作:

@Html.RadioButtonFor(item.Description, item.Selected, new {@class = "radio__input" })

它只是项目下的红线。描述无法解析项目。

如果我输入 <p>item.Description</> 只是为了测试我的 if 语句,那么这实际上就是渲染的内容,而不是我期望的项目描述。

【问题讨论】:

    标签: c# razor


    【解决方案1】:

    我相信method参数需要一个lambda,比如:

    @Html.RadioButtonFor(item => item.Description, new {@class = "radio__input" })
    

    【讨论】:

    • 仍然收到同样的错误。当我在这种情况下查看 item 时,似乎 item 被父级忽略了。即它想去 ModelBasket.AvailableOptions
    • 道歉 - 我认为 MVC 5.2 不支持我的重载示例。此处列出了支持的重载:docs.microsoft.com/en-us/dotnet/api/…。基本上,您必须提供 object 值 - 然后有三个选项:为 htmlAttributes 提供 objectIDictionary(或不提供属性)。
    【解决方案2】:

    试试这个,添加选中的选项。 @Html.RadioButtonFor(item => item.Description, new {@class = "radio__input", @checked = item.selected })

    【讨论】:

    • 描述下方和@checked = 的项目上的红线
    • 试试 "@checked="checked" 看看单选按钮是否被选中,如果它工作你必须像 @checked=item.selected ? "checked":""
    • 还是和以前一样。不喜欢物品描述
    • 使用这个@Html.RadioButtonFor(item=> item.selected,item.selected, new {@class= "radio__input", id=item.Description}) 和标签使用'labelFor '。希望这次能成功
    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多