所选单选按钮值未随MVC

所选单选按钮值未随MVC

本文介绍了所选单选按钮值未随MVC 5中的回发数据一起传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Html.RadioButtonFor进行列表中的项目选择,但这会导致名称属性冲突.这是我的样本模型

I used Html.RadioButtonFor for item selection in list but it causes name attribute conflict. here is my sample model

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;}
    ---
}

视图模板将如下所示

@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>
}

但这会导致名称属性冲突(选择了多个选项,而不是单个选项)

But this cause name attribute conflict (multiple options selected instead of single)

任何人都可以帮助我解决此问题.

Can anyone help me fix this problem.

推荐答案

我的问题是通过将isSelected属性从列表转移到模型来解决的,因为一次只能选择一个选项,因此无需在列表中使用.

My issue fixed by shifting isSelected attribute from list to model as only one option selected at a time therefore no need to use within list.

修改后的模型将如下所示.

Modified model will look like this.

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;}
    ---
}

修改后的视图将如下所示

Modified view will look like this

@for (var i = 0; i <= Model.DataList.Count - 1; i++)
{
       <label class="radio">@Html.RadioButtonFor(m =>Model.isSelected, Model.Videos[i].id)</label>
}

现在,此代码可以很好地与单选按钮选择和发布数据完美结合.

Now this code works fine perfectly with both radio button selection and posting data.

这篇关于所选单选按钮值未随MVC 5中的回发数据一起传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 09:26