我使用 Html.RadioButtonFor 在列表中选择项目,但它会导致名称属性冲突。这是我的示例模型
public class DataList {
public List<Videos> { set; get;}
}
public class Videos {
public int isSelected {set; get;}
public int id {set; get;}
public string Title { set; get;}
---
}
View 模板看起来像这样
@for (var i = 0; i <= Model.DataList.Count - 1; i++)
{
<label class="radio">@Html.RadioButtonFor(m =>Model.Videos[i].isSelected, Model.Videos[i].id)</label>
}
但这会导致名称属性冲突(选择多个选项而不是单个选项)
谁能帮我解决这个问题。
请您参考如下方法:
我的问题通过将 isSelected 属性从列表转移到模型来解决,因为一次只选择一个选项,因此不需要在列表中使用。
修改后的模型看起来像这样。
public class DataList {
public int isSelected {set; get;}
public List<Videos> Videos { set; get;}
}
public class Videos {
public int id {set; get;}
public string Title { set; get;}
---
}
修改后的 View 看起来像这样
@for (var i = 0; i <= Model.DataList.Count - 1; i++)
{
<label class="radio">@Html.RadioButtonFor(m =>Model.isSelected, Model.Videos[i].id)</label>
}
现在这段代码可以很好地处理单选按钮选择和发布数据。
