具有非布尔值的MVC CheckBoxList模型绑定 [英] MVC CheckBoxList model binding with non boolean

查看:67
本文介绍了具有非布尔值的MVC CheckBoxList模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to bind a List to CheckBox and get the selected values. I need to display two such Checkbox tables and have to retrieve both the IDs.

下面是我的ViewModel

Below is my ViewModel

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

}

这三个都具有这两个值 1. ID 2.名称(在这种情况下,角色-ID,RoleName |对于证券-ID,SecurityName ...) //添加第三个类型为bool的属性,以便仅使用这两个复选框,然后您会将其回传 这些没有没有任何布尔

All these three have these two values 1. ID 2. Name (In this case for Role - ID, RoleName | for Securities - ID, SecurityName ...) //add 3rd property of type bool isselected in order to work eith checkboxes only then you will get it posted back These don't have any boolean values

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

By using this ViewModel I'm binding these items using the below method...

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文件是..

My corresponding cshtml file is..

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

我得到以下输出,

我想将检查后的值作为模型.

I would want to get the checked values as a model.

我有一个类似HttpPost的方法,但是它返回空值.

I have a HttpPost method like this, but it returns null values.

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

请让我知道如何获取模型中的检入值?

Please let me know how do I get the checked in values in the model?

非常感谢!

推荐答案

只需在上面充实我的评论...

Just to flesh out my comment above...

对于checkboxList-视图模型应同时包含identifier属性和boolean selected属性.如果尚未实现,则可以扩展或创建一个新的ViewModel类来实现此目的-将现有模型映射到该特定的ViewModel.

For a checkboxList - the viewmodel should include both an identifier property, and boolean selected property. If it doesn't already then either extend or create a new ViewModel class to fulfil this purpose - map your existing model to this specific 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;}
}

您的视图: 请注意,除了每个用户角色/用户安全性ID属性的复选框之外,还包含了Html.HiddenFor()复选框,这使MVC在回发后可以绑定ID属性.

Your View: Note that in addition to the checkboxes Html.HiddenFor()has been included for each of the UserRole/UserSecurity ID properties, which allows MVC to bind the ID properties after postback.

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

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

and your controller should now use the new model above too!

这篇关于具有非布尔值的MVC CheckBoxList模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆