动态填充多个复选框并将选中的值存储到mvc 4中的表单中的一个模型字段

时间:2015-08-05 12:24:54

标签: c# asp.net-mvc forms asp.net-mvc-4 checkbox

我正在创建mvc应用程序。在数据库中,我有一个用于用户组的表,另一个用于产品字段的表。

我创建了表单以添加新产品字段。当我添加新产品字段时,我想分配用户组(这些用户组可以动态更改)。我想使用下面的图片复选框

enter image description here

这是产品领域的模型类

public partial class ProductField
{
    public string ProductFieldID { get; set; }
    public string ProductTypeEn { get; set; }
    public string ProductTypeAr { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { get; set; }
    public string ProdcutFieldDiscriptionEn { get; set; }
    public string ProductFieldDiscriptionAr { get; set; }
    public string UserGroup { get; set; }
    public string Status { get; set; }

}

这是查看页面

        <div class="form-group">
        @Html.LabelFor(model => model.ProductFieldID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ProductFieldID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ProductFieldID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ProductFieldNameEn, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ProductFieldNameEn, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ProductFieldNameAr, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ProductFieldNameAr, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ProductFieldNameAr, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ProdcutFieldDiscriptionEn, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ProdcutFieldDiscriptionEn, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ProdcutFieldDiscriptionEn, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ProductFieldDiscriptionAr, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ProductFieldDiscriptionAr, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ProductFieldDiscriptionAr, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

当我检查用户组时,这些valuse应该存储在Product field表中的一个字段下(&#34; User_Group&#34; nvarchar字段)

这可能吗?真的很感激可以建议一种方法来实现这个目标

1 个答案:

答案 0 :(得分:1)

对于每个复选框,添加相应的模型属性并生成UserGroup计算字段

public partial class ProductField
{
    public string ProductFieldID { get; set; }
    public string ProductTypeEn { get; set; }
    public string ProductTypeAr { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { get; set; }
    public string ProdcutFieldDiscriptionEn { get; set; }
    public string ProductFieldDiscriptionAr { get; set; }
    public string UserGroupSerialized { 
       get
       { 
            return JsonConvert.SerializeObject(UserGroups, Formatting.Indented);
       }
    }

    public IList<KeyValuePair<string,bool>> UserGroups { get; set; }
    public string Status { get; set; }
}

查看:

@for(var i=0; i<UserGroups.Count; i++)
{
    <div class="form-group">
        @Html.Label("", Model.UserGroups[i].Key , htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.HiddenFor(model => model.UserGroups[i].Key)
            @Html.EditorFor(model => model.UserGroups[i].Value , new { htmlAttributes = new { @class = "form-control" } })                
        </div>
    </div>
}