MVC 4 Razor中的多个单选按钮组

时间:2014-03-04 14:19:14

标签: asp.net-mvc-4 razor radio-button radio-group

我需要在表单中有多个单选按钮组,如下所示:
enter image description here

我知道通过为每个组指定相同的“ name ”html属性来完成它。
无论其
使用html helper时,MVC不允许您指定自己的name属性:

@Html.RadioButtonFor(i => item.id, item.SelectedID, new { Name = item.OptServiceCatId })  

因为它会查看每个标记的“名称”属性(而不是“ id ”)以将表单映射/绑定到模型控制器收到等等。

有人说用相同的“GroupName”属性指定每个都会解决问题,但它也不起作用。

那么,有什么方法可行吗?

修改
这是我的观点(简化):

@model Service_Provider.ViewModels.SelectOptServicesForSubServiceViewModel

@foreach (var cat in Model.OptServices)
{
  //A piece of code & html here
  @foreach (var item in cat.OptItems.Where(i => i.MultiSelect == false))
  {
     @Html.RadioButtonFor(i => item.id, item.SelectedID, new { GroupName = item.OptServiceCatId })
<br />
  }    
}

注意:
我的模型是List<OptServices>

public List<OptServices> Cats {get; set;}

OptServices里面有List OptItems

public class OptServices
{
//a few things
public List<OptItems> Items {get; set;}
}

5 个答案:

答案 0 :(得分:28)

您只需将组绑定到模型中的其他项目

即可
@Html.RadioButtonFor(x => x.Field1, "Milk")
@Html.RadioButtonFor(x => x.Field1, "Butter")

@Html.RadioButtonFor(x => x.Field2, "Water")
@Html.RadioButtonFor(x => x.Field2, "Beer")

答案 1 :(得分:18)

好的,这是我如何解决这个问题

我的模型是类别list。每个类别都包含{<1>}个子类别 考虑到这一点,每次在foreach循环中,每个list都会将其类别的ID(这是唯一的)作为其名称属性。
我还使用RadioButton代替Html.RadioButton

这是最终的'工作'伪代码:

Html.RadioButtonFor

结果是:

@foreach (var cat in Model.Categories)
{
  //A piece of code & html here
  @foreach (var item in cat.SubCategories)
  {
     @Html.RadioButton(item.CategoryID.ToString(), item.ID)
  }    
}

请注意,我没有将所有这些单选按钮组放在表单中。而且我不知道这个解决方案是否仍然可以在表单中正常工作。

感谢所有帮助我解决此问题的人;)

答案 2 :(得分:3)

我修复了一个类似的问题,用SelectList中的文本/值对构建一个RadioButtonFor。我使用ViewBag将SelectList发送到View,但您也可以使用模型中的数据。我的网络应用程序是一个博客,我必须在撰写新帖子时用一些类型的文章构建一个RadioButton。

下面的代码很简单。

List<SelectListItem> items = new List<SelectListItem>();

Dictionary<string, string> dictionary = new Dictionary<string, string>();

dictionary.Add("Texto", "1");
dictionary.Add("Foto", "2");
dictionary.Add("Vídeo", "3");

foreach (KeyValuePair<string, string> pair in objBLL.GetTiposPost())
{
    items.Add(new SelectListItem() { Text = pair.Key, Value = pair.Value, Selected = false });
}

ViewBag.TiposPost = new SelectList(items, "Value", "Text");

在视图中,我使用foreach来构建一个radiobutton。

<div class="form-group">
    <div class="col-sm-10">
        @foreach (var item in (SelectList)ViewBag.TiposPost)
        {
            @Html.RadioButtonFor(model => model.IDTipoPost, item.Value, false)
            <label class="control-label">@item.Text</label>
        }

    </div>
</div>

请注意,在提交表单后,我使用 RadioButtonFor 来捕获用户在Controler中选择的选项。我还必须将item.Text放在RadioButtonFor之外,以显示文本选项。

希望它有用!

答案 3 :(得分:0)

您可以使用词典进行映射 假设牛奶,黄油,奶酪是A组(ListA) 水,啤酒,葡萄酒是B组

Dictonary<string,List<string>>) dataMap;
dataMap.add("A",ListA);
dataMap.add("B",ListB);

在View中,您可以在dataMap中预览Keys并处理您的操作

答案 4 :(得分:0)

我能够将您在示例中描述的name属性用于正在处理的循环,并且该循环有效,也许是因为我创建了唯一的ID?我仍在考虑是否应该切换到编辑器模板,而不是在另一个答案中的链接中提到。

    @Html.RadioButtonFor(modelItem => item.Answers.AnswerYesNo, "true", new {Name = item.Description.QuestionId, id = string.Format("CBY{0}", item.Description.QuestionId), onclick = "setDescriptionVisibility(this)" }) Yes

    @Html.RadioButtonFor(modelItem => item.Answers.AnswerYesNo, "false", new { Name = item.Description.QuestionId, id = string.Format("CBN{0}", item.Description.QuestionId), onclick = "setDescriptionVisibility(this)" } ) No
相关问题