简体   繁体   English

剃刀视图@ Html.RadioButtonFor

[英]razor view @Html.RadioButtonFor

I have the following bit of Razor code: 我有以下剃刀代码:

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

      }
    }
}

each item has a Selected property and description property. 每个项目都有一个Selected属性和description属性。 I want to create a radio butoon for each item but if I try and do the following in my if statement: 我想为每个商品创建一个广播按钮,但是如果我尝试在if语句中执行以下操作:

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

it just red lines under item.Description with cannot resolve item. 它只是item下的红线。带有的描述无法解析item。

Just to test in my if statement if I put <p>item.Description</> then this is literally what gets rendered and not the item description as I'd expect. 只是在我的if语句中测试是否放了<p>item.Description</>那么从字面上看这就是要呈现的内容,而不是我期望的项目描述。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM