简体   繁体   English

如何处理ASP.NET MVC3中的多个单选按钮?

[英]How to handle multiple radiobuttons in asp.net mvc3?

I have table like: 我有这样的表:

id value

3 var1
6 var2
81 var3

so I need display two radiobutton's for each record like: 所以我需要为每个记录显示两个单选按钮,例如:

var1
O Yes
O No

var2
O Yes
O No

var3
O Yes
O No

Where "O" is radiobutton. 其中“ O”是单选按钮。

But I have no idea how I can handle it in controller. 但是我不知道如何在控制器中处理它。 There can be more records of course. 当然可以有更多的记录。 How I should named them or something else. 我应该如何命名它们或其他名称。 Any help would be appreciated. 任何帮助,将不胜感激。

Have you tried some approach like this? 您是否尝试过这种方法?

//Model //模型

public class MyModel
{
   public int SomeProperty { get; set; }
   public int SomeOtherProperty { get; set; } 

   public IList<MyDetails> RadioButtonList{ get; set; }
}

public class MyDetails
{
   public string Name { get; set; }
   public string Id { get; set; }
}

// Controller //控制器

public ActionResult Index()
{
  MyModel myModel = new MyModel()
  {
   RadioButtonList = getListFromDB();
   SomeProperty  = valuse       
  };

  return View(myModel);
}

//View //视图

@foreach (var item in Model.RadioButtonList)
{
   <b>@item.Name</b>
   @Html.RadioButton("@item.Id", "0", true); <span> Yes </span><br />
   @Html.RadioButton("@item.Id", "1", false); <span> No </span><br />   
}

In order for this to work, you need to give each pair of radio button a distinct group name. 为了使它起作用,您需要为每对单选按钮赋予一个不同的组名。 This ensures, that only yes or no for each combination can be selected. 这样可以确保每个组合只能选择是或否。

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

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