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

问题描述 投票:0回答:2

我是在 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)
    {
  }
asp.net-mvc-4
2个回答
0
投票

替换你的 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" })    
}

0
投票

我在MVC5中工作,我有一些以单选按钮形式存在的mcqs问题,这些问题从数据库中随机获取,下面是我的`[表("Mcqs")]

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

我想问的是,如何获得洗牌问题的单选按钮的值?

© www.soinside.com 2019 - 2024. All rights reserved.