如何在MVC4中保存选定的单选按钮值

时间:2013-07-04 07:24:17

标签: asp.net-mvc-4

我在MVC4工作。在此我使用以下代码radio buttons

型号:

 public class PlatInspHistoryModels
 {
  public List<RadioButtonItem> RadioButtonList { get; set; }
  public string SelectedRadioButton { get; set; }
}

public class RadioButtonItem
{
    public string Name { get; set; }
    public bool Selected { get; set; }
    public string Value { get; set; }
    public bool Visible { get; set; }
}

控制器:

  public ActionResult Index()
    {
        var viewModel = new PlatInspHistoryModels
        {
            RadioButtonList = new List<RadioButtonItem>
                                                      {
                                                          new RadioButtonItem
                                                              {
                                                                  Name = "Topside", Value = "T",Selected = true,Visible = true
                                                              },
                                                          new RadioButtonItem
                                                              {
                                                                  Name="Underwater", Value = "U",Selected = false,Visible = true
                                                              }
                                                      }
        };
        return View(viewModel);
    }

查看:

   @using (Html.BeginForm("Index", "PlatInspHistory", FormMethod.Post, new { id = "form" }))
     {
    <table cellpadding="4" cellspacing="4">
                <tr>
                    <td>

                        foreach (Cairs2.Models.RadioButtonItem item in Model.RadioButtonList)
                        {
                            @Html.DisplayFor(i => item.Name)
                            @Html.RadioButton("PlatInspHistoryModels.SelectedRadioButton", item.Value, item.Selected, new { @class = "formCheckbox", tabindex = "1" })    
                        }


                    </td>
  </tr>
            </table>
  }

问题:

从上面的代码我可以将单选按钮绑定为列表。但是我如何在下面给出的save event上获得选定的无线电值:

  [HttpPost]
    public ActionResult Index(PlatInspHistoryModels model)
    {
  }

2 个答案:

答案 0 :(得分:1)

使用foreach循环替换for循环,并使用强类型帮助程序:

for (var i = 0; i < Model.RadioButtonList.Count; i++)
{
    @Html.DisplayFor(x => x.RadioButtonList[i].Name)
    @Html.HiddenFor(x => x.RadioButtonList[i].Name)
    @Html.RadioButtonFor(x => x.SelectedRadioButton, Model.RadioButtonList[i].Value, new { @class = "formCheckbox", tabindex = "1" })    
}

答案 1 :(得分:0)

我在MVC5工作,我有从单选按钮中随机取出的单选按钮形式的mcqs问题,这是我的[表格(&#34; Mcqs&#34;)]

public class MCQS   
      {         [Key]     
        public int Qid { get; set; }  
       [Required(ErrorMessage = "Please Enter Skill.")]     
        public string Skill { get; set; }      
       [Required(ErrorMessage = "Please Enter Question.")]     
        public string Question { get; set; }      
       [Required(ErrorMessage = "Please Enter Level.")]    
        public string Levels { get; set; }     
       [Required(ErrorMessage = "Please Enter Options.")]   
       public string AnsOpt1 { get; set; }   
       [Required(ErrorMessage = "Please Enter Options.")]    
      public string AnsOpt2 { get; set; }       
      [Required(ErrorMessage = "Please Enter Options.")]    
       public string AnsOpt3 { get; set; }       
     [Required(ErrorMessage = "Please Enter Options.")]   
      public string AnsOpt4 { get; set; }      
     [Required(ErrorMessage = "Please Set Weightage.")]

和控制器

  public ActionResult Index()     
 {             return View((from a in ne.Mcqs orderby Guid.NewGuid() select a).Take(2).ToList());          } 

并查看

 @foreach (var item in Model)   
{<tr>           
<td>
@Html.DisplayFor(modelItem=>item.Qid)</td>       
  <td>  @Html.DisplayFor(modelItem => item.Question)</td> <td>           
      @Html.RadioButton("skillsq1", new { id = "1"}).   
          @Html.DisplayFor(modelItem => item.AnsOpt1)</td><td>          
   @Html.RadioButton("skillsq1", new { id = "2"})   
      @Html.DisplayFor(modelItem => item.AnsOpt2) </td><td>       
  @Html.RadioButton("skillsq1", new { id = "3" })   
@Html.DisplayFor(modelItem => item.AnsOpt3) </td> <td>     

@Html.RadioButton("skillsq1", new { id = "4" }) 
 @Html.DisplayFor(modelItem => item.AnsOpt4) </td> </tr>}      

我想问一下如何获得洗牌问题的已检查单选按钮的值