【问题标题】:Bind multiple columns of radio buttons in a table (grouped for each row)绑定表格中的多列单选按钮(为每一行分组)
【发布时间】:2012-11-14 23:03:58
【问题描述】:

(我不是 MVC 专家,只是说) 我需要渲染一个包含 4 列的<table>

  • 名称
  • 无法访问
  • 查看器
  • 文件管理器

    最后3个是布尔值,需要绑定一个单选按钮,这个单选按钮需要对每一行进行分组。

    我知道在视图中显示它的许多不同方式,但不确定确保数据正确提交回控制器的最佳方式。

    型号:

    public class DetailModel 
    { 
        //this displays the user information
        public Users_SelectByUserId User { get; set; }
        //This is what builds the Table with the 4 columns.
        public List<UserMinistryRef_SelectByUserID> MinistryRef { get; set; }
    
    }
    

    --

    public class UserMinistryRef_SelectByUserID
    {
        public int UserRecordId { get; set; }
        public int MinistryId { get; set; }
        public bool Minadmin { get; set; }
        public bool Updater { get; set; }
        public bool Viewer { get; set; }
        public string mname { get; set; }
    }
    

    什么是最好的显示方式,并且有能力将它们正确地填充回控制器?我已经尝试遍历 MinistryRef 集合,但我没有得到太多的运气,在循环集合时无法让它与 Razor 一起工作,并且绑定到第 3 方网格控件只是结果相当忙碌。

    我不确定我是否走在正确的轨道上......

     @foreach (UserMinistryRef_SelectByUserID ministry in Model.MinistryRef)
                { 
                    <tr>
                        <td>@ministry.mname</td>
                        ***NOte the below @Helpers are not supposed to work, just trying to show what I'm hoping to achieve.
                        <td>
                            @Html.RadioButtonFor(Model.MinistryRef.MinAdmin == true)
                        </td>
                        <td>
                           Html.RadioButtonFor(Model.MinistryRef.Viewer == true)
                        </td>
                        <td>
                            Html.RadioButtonFor(Model.MinistryRef.Viewer == false && Model.MinistryRef.MinAdmin == false)
                        </td>
                    </tr>
                }           
    

    有什么想法吗?

  • 【问题讨论】:

      标签: c# .net asp.net-mvc-3 razor


      【解决方案1】:

      也许您可以像这样使用 int 抽象来控制单选按钮组:

      型号

      public class UserMinistryRef_SelectByUserID
      {
       public int UserRecordId { get; set; }
       public int MinistryId { get; set; }
       public bool Minadmin { get; set; }
       public bool Updater { get; set; }
       public bool Viewer { get; set; }
       public string mname { get; set; }
       public int AccessRights { get; set; } //0 = viewer, 1 = updater, 2 = minadmin
      }
      

      查看

      @{ int count = 0; }
      @foreach (UserMinistryRef_SelectByUserID ministry in Model.MinistryRef)
      {
       <tr>
        <td>@ministry.mname</td>
        <td>
         <input name="MinistryRef[@(count)].AccessRights" type="radio" value="0" @if(ministry.Viewer){<text>checked="checked"</text>} /> 
        </td>
        <td>
         <input name="MinistryRef[@(count)].AccessRights" type="radio" value="1" @if(ministry.Updater){<text>checked="checked"</text>} /> 
        </td>
        <td>
         <input name="MinistryRef[@(count)].AccessRights" type="radio" value="2" @if(ministry.Minadmin){<text>checked="checked"</text>} /> 
        </td>
       </tr>
       count++;
      }
      

      控制器

      [HttpPost]
      public ActionResult action( DetailModel vm )
      {
       if (ModelState.IsValid)
       {
        foreach( var min in vm.MinistryRef )
        {
         switch( min.AccessRights )
         {
          case 0: /* Viewer */
          case 1: /* Updater */
          case 2: /* Minadmin */
         }
        }
       }
      
       return RedirectToAction("SomeHttpGet");
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-15
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 1970-01-01
        • 2012-07-31
        • 2018-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多