MVC CheckBoxList模型绑定非布尔值

时间:2014-12-18 09:40:53

标签: c# asp.net-mvc asp.net-mvc-4 checkboxlist

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

以下是我的ViewModel

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

}

所有这三个都有这两个值 身份证 2.名称(在本例中为Role-ID,RoleName | for Securities - ID,SecurityName ......) //添加bool isselected类型的第3个属性,以便只使用复选框,然后才能将其发回 这些布尔值

通过使用此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);
}

我对应的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>
}

我得到以下输出, enter image description here

我希望将选中的值作为模型。

我有这样的HttpPost方法,但它返回空值。

[HttpPost]
public ActionResult AddingRoleSecurity(ViewModel_Role_Security model)
{
    return View();
}

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

非常感谢!

4 个答案:

答案 0 :(得分: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;}
}

您的观点: 请注意,除了复选框之外,每个UserRole / UserSecurity ID属性都包含Html.HiddenFor(),这允许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>

并且您的控制器现在也应该使用上面的新模型!

答案 1 :(得分:1)

我不想表现得像詹姆斯出色的工作,但我能做的就是新答案。由于我不理解的原因,我的编辑纠正了代码中的编译错误 - 因为没有纠正关键问题而被拒绝。所以我发布这个作为一个完整的工作答案。

模特:

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

观点:

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

答案 2 :(得分:0)

我会使用https://www.nuget.org/packages/BeginCollectionItem/

这将帮助您使用GUID

获得正确的绑定序列

最好的问候 Burim

答案 3 :(得分:0)

你应该为你的两个模型添加第三个bool属性,

  public bool IsSelected { get; set; }

并将其默认设置为false,当您选中相应的复选框时,将在回发时将其设置为true 根据真实的价值观,您可以使用相应的选定值和文本做任何您想做的事情

这是我的代码,我已经使用了一些实现相同的地方:

                    <td colspan="4" style="-webkit-column-count: 3; -moz-column-count:3; -ms-column-count:3; -o-column-count: 3; column-count: 3">
                        @{int i = 0;
                        }
                        @foreach (var item in Model.SurfaceTypeList)
                        {

                            <input name="SurfaceTypeList[@i].Text" type="hidden" value="@item.Text" />
                            <input name="SurfaceTypeList[@i].Value" type="hidden" value="@item.Value" />
                            <input data-val="true" id="SurfaceTypeList_@(i)__IsSelected" name="SurfaceTypeList[@i].IsSelected" type="checkbox" class="SurfaceType" value="true" checked="@item.IsSelected" />
                            @Html.Label(item.Text)
                            <br /><br />
                            i++;
                        }


                    </td>
                    <td colspan="2"></td>

模型包含一个属性:

public List<SurfaceType> SurfaceTypeList { get; set; }
public class SurfaceType
{
public bool IsSelected { get; set; }
    public string Text { get; set; }
    public string Value { get; set; }
}

以下是我检索文本/值的方法:

  foreach (var item in modelData.SurfaceTypeList)
            {
                if (item.IsSelected)
                {
                    var surfaceType = new XElement("SurfaceType");
                    var id = new XElement("Id");
                    id.Add(item.Value);
                    surfaceType.Add(id);
                    var i = id.Value;
                    surfaceTypeList.Add(surfaceType);
                }

            }