0

我正在工作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)
    {
  }
4

2 回答 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" })    
}
于 2013-07-04T07:27:40.667 回答
0

我在 MVC5 中工作,我有单选按钮形式的 mcqs 问题,这些问题是从数据库中随机获取的,这里是我的`[Table("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>}      

我想问一下我如何获得随机问题的选中单选按钮的值

于 2016-02-13T08:36:20.183 回答