【问题标题】:CheckBoxList multiple selections: difficulty in model bind backCheckBoxList 多选:模型绑定回困难
【发布时间】:2011-01-27 07:35:44
【问题描述】:

我的课如下

 public class UserRoleModel
{
    public string Role { get; set; }
    public bool UserRole { get; set; }
}

public UserRoleModel[] UserRoles { get; set; }


我的控制器如下:

 public ActionResult CreateUser()
     {
         UserDetailsModel model = new UserDetailsModel();
         return View(model);
     }

     [HttpPost]
     public ActionResult CreateUser(UserDetailsModel model)
     {

         return View(model);
     }

在我看来,我有

    >@foreach (var item in Model.UserRoles)      
    { 

    name = "UserRoles"+ ".Value["+ i + "]"; 
    id= "UserRoles" + "_Value[" + i++ + "]";
    selected = item.UserRole ? "checked=\"checked\"" : ""; 

        <p>
        <input type="checkbox" name="@name" id="@id" @selected value="true" /> 
        <label for="@id">@item.Role</label> 
        <input type="hidden" name="@name" value="false" /> 
        </p> 
  } 

尽管在我的视图中相应地显示了值,但 UserRoles 没有模型绑定。我错过了什么或者有更好更清洁的方法吗?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    这些事情可以通过编辑器模板很好地实现。它们还避免您在视图中编写意大利面条式代码。示例:

    型号:

    public class UserDetailsModel
    {
        public IEnumerable<UserRoleModel> Roles { get; set; }
    }
    
    public class UserRoleModel
    {
        public string Role { get; set; }
        public bool UserRole { get; set; }
    }
    

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new UserDetailsModel
            {
                // Fill with some dummy stuff
                Roles = Enumerable.Range(1, 5).Select(x => new UserRoleModel
                {
                    Role = "role " + x,
                    UserRole = false
                })
            });
        }
    
        [HttpPost]
        public ActionResult Index(UserDetailsModel model)
        {
            return View(model);
        }
    }
    

    查看(~/Views/Home/Index.cshtml):

    @model AppName.Models.UserDetailsModel
    @using (Html.BeginForm())
    { 
        @Html.EditorFor(x => x.Roles)
        <input type="submit" value="OK" />
    }
    

    编辑器模板(~/Views/Home/EditorTemplates/UserRoleModel.cshtml):

    @model AppName.Models.UserRoleModel
    @Html.CheckBoxFor(x => x.UserRole)
    @Html.LabelFor(x => x.Role, Model.Role)
    @Html.HiddenFor(x => x.Role)
    

    这就是我所说的干净的东西。

    【讨论】:

    • 在我实际尝试之前我没有注意到的一件事 - @Html.EditorFor(x=>x.Roles) 实际上会遍历集合。确实干净。
    • 太棒了。对于我正在寻找的东西,这看起来是一个很好的解决方案(最后)。明天肯定会尝试一下。找不到我满意的东西,也绑定在 HttpPost 上;可能是在寻找错误的东西。大多数解决方案都需要在控制器的 HttpPost 操作中添加一个额外的数组或其他参数。谢谢达林。
    • 现在测试了它,它也适用于我的情况。但是我将LabelFor 更改为使用与CheckBoxFor 相同的属性,以便单击标签也会触发复选框。在本例中为:@Html.LabelFor(x =&gt; x.UserRole, Model.Role)
    • 这可以做得更深一层吗?例如,一页包含用户列表以及其角色的复选框列表? (不是实际案例,而是类似的架构
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多