MVC CheckBoxList模型绑定非布尔值

ind*_*xxo 4 c# asp.net-mvc checkboxlist asp.net-mvc-4

我想将List绑定到CheckBox并获取所选值.我需要显示两个这样的Checkbox表,并且必须检索这两个ID.

下面是我的ViewModel

public partial class ViewModel_Role_Security
{
    public List<Roles> oRoleMaster { get; set; }
    public List<Securities> oSecurityMaster { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

所有这三个都有这两个值1. ID 2.名称(在这种情况下为Role-ID,RoleName | for Securities - ID,SecurityName ...)//添加bool类型的第3个属性isselected只为了工作eith复选框那么你会得到它回来这些没有任何布尔

通过使用此ViewModel,我使用以下方法绑定这些项目...

public ActionResult AddingRoleSecurity()
{        
    ListRoles = new List<Roles>();
    ListSecurities = new List<Securities>();  //And then populate them with data ...

    var model = new ViewModel_Role_Security();
    model.oRoleMaster = ListRoles;
    model.oSecurityMaster = ListSecurities;
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

我对应的cshtml文件是..

@model KnackWFM.BusinessEntities.ViewModel_Role_Security

@using (Html.BeginForm())
{
    <div class="contentsecurity">

        <div class="User_role">
            <p class="Security_role">User Role</p>

            @for (int i = 0; i < Model.oRoleMaster.Count; i++)
            {
                <input id="@Model.oRoleMaster[i].RoleID" name="@Model.oRoleMaster[i].RoleName" type="checkbox" value="@(Model.oRoleMaster[i].RoleName)" />
                <label for="@Model.oRoleMaster[i].RoleID">@Model.oRoleMaster[i].RoleName</label>
                <br />
                @Html.CheckBoxFor(Model.oRoleMaster[i].RoleID.selec)
            }

        </div>

        <div class="User_Page">
            <p class="Security_role">Role Security</p>

            @for (int i = 0; i < Model.oSecurityMaster.Count; i++)
            {
                <input id="@Model.oSecurityMaster[i].SecurityID" name="@Model.oSecurityMaster[i].SecurityName" type="checkbox" value="@(Model.oSecurityMaster[i].SecurityName)" />
                <label for="@Model.oSecurityMaster[i].SecurityID">@Model.oSecurityMaster[i].SecurityName</label>
                <br />
            }

        </div>
        <div class="bottombuttonsecurity">
            <button type="submit" id="btnSave" name="Command" value="Save" style="background-color: #3d3c4c;border-radius: 8px;color: white;padding: 5px;border: 1px solid #3d3c4c;">Save</button>
        </div>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出, 在此输入图像描述

我想将选中的值作为模型.

我有这样的HttpPost方法,但它返回null值.

[HttpPost]
public ActionResult AddingRoleSecurity(ViewModel_Role_Security model)
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

请告诉我如何获取模型中的已检入值?

非常感谢你!

Jam*_*s S 10

只是为了充实我上面的评论......

对于checkboxList - viewmodel应包含标识符属性和布尔选择属性.如果它尚未扩展或创建新的ViewModel类以实现此目的 - 将现有模型映射到此特定视图模型.

即 - 您的模型类

public class UserRole
{
  public int RoleID {get; set;}
  public string RoleName {get; set;}
  public bool Selected {get; set;}
}

public class UserSecurity
{
  public int SecurityID {get; set;}
  public string SecurityName {get; set;}
  public bool Selected {get; set;}
}

public class UserRoleAndSecurityModel
{
  public List<UserRole> RoleMaster {get; set;}
  public List<UserSecurity> SecurityMaster {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

您的视图:请注意,除了Html.HiddenFor()每个UserRole/UserSecurity ID属性都包含复选框之外,还允许MVC在回发后绑定ID属性.

@model UserRoleAndSecurityModel   

@using (Html.BeginForm())
{
    <div class="contentsecurity">  
        <div class="User_role">
            <p class="Security_role">User Role</p>

            @for (int i = 0; i < Model.RoleMaster.Count; i++)
            {
                @Html.CheckBoxFor(m => m.RoleMaster[i].Selected)
                @Html.HiddenFor(m => m.RoleMaster[i].RoleId)
                @Html.LabelFor(m => m.RoleMaster[i].Selected, 
                                    Model.RoleMaster[i].RoleName)
                <br />
            }
        </div>

        <div class="User_Page">
            <p class="Security_role">Role Security</p>

            @for (int i = 0; i < Model.SecurityMaster.Count; i++)
            {
                @Html.CheckBoxFor(m => m.SecurityMaster[i].Selected)
                @Html.HiddenFor(m => m.SecurityMaster[i].SecurityId) 
                @Html.LabelFor(m => m.SecurityMaster[i].Selected, 
                                    Model.SecurityMaster[i].SecurityName)
                <br />

            }
        </div>
        <div class="bottombuttonsecurity">
            <button type="submit" id="btnSave" name="Command" value="Save" style="background-color: #3d3c4c;border-radius: 8px;color: white;padding: 5px;border: 1px solid #3d3c4c;">Save</button>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

你的控制器现在也应该使用上面的新型号了!

  • 您不应总是希望能够将实体框架类直接绑定到Views - MVC模型应该包含具有要与View交换的信息的属性.创建仅用于单个视图的模型是完全可以接受的,并让您的控制器创建一个新实例,然后从实体框架模型映射所需的属性 (3认同)