CheckBoxFor不绑定到模型 [英] CheckBoxFor not binding to model

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

问题描述

我的应用程序的工作方式如下:用户从下拉列表中选择一个项目,并加载部分视图.返回部分视图的控制器操作从数据库中提取数据,并使用数据库中存在的值填充部分视图中的复选框.为此,很明显,模型是通过部分视图返回的.一切正常.

My application works like this: A user chooses a project from a dropdown list and it loads a partial view. The controller action that returns the partial view pulls data from a database and populates the checkboxes in the partial view with the values that exist in the database. To do that, obviously the model is returned with the partial view. This is working just fine.

现在,如果用户更改复选框的值并再次提交表单,则我认为复选框的新值将绑定到模型,因此,我可以使用它来用新值更新数据库.不幸的是,即使选中该复选框,所有内容也将返回"false".我确实知道,如果对复选框未执行任何操作,则会提交错误的值(即使它已预先填充有数据库中现有值的选中标记),我也会担心自己稍后进行修复,但我试图弄清楚为什么主动检查的框未绑定到模型.我不确定这是怎么回事,因为根据我阅读的所有内容,我的CheckBoxFor助手看起来正确无误.

Now, if a user changes the value of the checkbox and submits the form again, I assumed the checkboxes' new values would be bound to the model and thus, I would be able to use it in updating the database with new values. Unforunately, everything is coming back as "false" even if the checkbox is checked. I do understand that if no action is taken on the checkbox it will submit a false value (even if it was pre-populated with a checkmark from an existing value in the db), I will concern myself with fixing that later, but I'm trying to figure out why the boxes that get actively checked are not binding to the model. I'm not sure what the deal is as my CheckBoxFor helpers look correct based on everything I've read.

模型

public class DataSharingModels
{
    public string ReferenceID { get; set; }
    public NBTC NBTCGroup { get; set; }
    public Contractors ContractorsGroup { get; set; }
    public Coordinators CoordinatorsGroup { get; set; }
    public NGO NGOGroup { get; set; }
    public Public PublicGroup { get; set; }
    public SelectList FA_RA_List { get; set; }

}

public class NBTC
{
    public Boolean NBTC_FA_Centroid { get; set; }
    public Boolean NBTC_FA_Bound { get; set; }
    public Boolean NBTC_RA_Centroid { get; set; }
    public Boolean NBTC_RA_Bound { get; set; }
    public Boolean NBTC_Spring_Sum { get; set; }
    public Boolean NBTC_Spring_Analysis { get; set; }
    public Boolean NBTC_Spring_Locate { get; set; }
    public Boolean NBTC_Fall_Sum { get; set; }
    public Boolean NBTC_Fall_Analysis { get; set; }
    public Boolean NBTC_Fall_Locate { get; set; }
    public Boolean NBTC_HabMon_Sum { get; set; }
    public Boolean NBTC_HabMon_Analysis { get; set; }
    public Boolean NBTC_HabMon_Locate { get; set; }
    public Boolean NBTC_HabMgmt_Sum { get; set; }
    public Boolean NBTC_HabMgmt_Analysis { get; set; }
    public Boolean NBTC_HabMgmt_Locate { get; set; }
    public Boolean NBTC_Inventory_Sum { get; set; }
    public Boolean NBTC_OpSvy_Sum { get; set; }
    public Boolean NBTC_OpSvy_Individ { get; set; }
}
//continues...

局部视图

@model xxx.Models.DataSharingModels

@using (Html.BeginForm("SetPermission", "DataSharing", FormMethod.Post, new { id = "dShareForm" }))
{
<table id="data-sharing-table">
    //a whole bunch of table set up
    <tr>
        <td rowspan="2">CIP Focal Area</td>
        <td>Centroid</td>
        <td><input type="checkbox" name="NBCIStaff" checked disabled /></td>
        <td>@Html.CheckBoxFor(m => m.NBTCGroup.NBTC_FA_Centroid, nbtcAttr)</td>
        <td>@Html.CheckBoxFor(m => m.ContractorsGroup.Contractors_FA_Centroid, contractAttr)</td>
        <td>@Html.CheckBoxFor(m => m.CoordinatorsGroup.Coordinators_FA_Centroid, coordAttr)</td>
        <td>@Html.CheckBoxFor(m => m.NGOGroup.NGO_FA_Centroid, ngoAttr)</td>
        <td>@Html.CheckBoxFor(m => m.PublicGroup.Public_FA_Centroid, publicAttr)</td>
    </tr>
    <tr>
        <td>Boundary</td>
        <td><input type="checkbox" name="NBCIStaff" checked disabled /></td>
        <td>@Html.CheckBoxFor(m => m.NBTCGroup.NBTC_FA_Bound, nbtcAttr)</td>
        <td>@Html.CheckBoxFor(m => m.ContractorsGroup.Contractors_FA_Bound, contractAttr)</td>
        <td>@Html.CheckBoxFor(m => m.CoordinatorsGroup.Coordinators_FA_Bound, coordAttr)</td>
        <td>@Html.CheckBoxFor(m => m.NGOGroup.NGO_FA_Bound, ngoAttr)</td>
        <td>@Html.CheckBoxFor(m => m.PublicGroup.Public_FA_Bound, publicAttr)</td>
    </tr>
    //it continues on like this
</table>
<button class="btn btn-default" type="submit">Submit</button>
}

控制器

[HttpPost]
public void SetPermission(DataSharingModels dsm)
{
    //do stuff
}

推荐答案

因此,事实证明问题出在我附加的变量中,这些变量用于将html属性添加到我的复选框中.我认为这样的事情:

So it turns out the problem was with the variables I was attaching to add html attributes to my checkboxes. I had something like this in my view:

nbtcAttr = new {@Name = "NBTC"}

这将覆盖使用CheckBoxFor生成的名称,而不是此名称:

This overrides the name that gets generated using CheckBoxFor so instead of this:

@Html.CheckBoxFor(m => m.NBTCGroup.NBTC_FA_Centroid) 
//generates:
//<input name="NBTCGroup.NBTC_FA_Centroid" data-val:"true" data-val-required:"The NBTC_FA_Centroid field is required" id="NBTCGroup_NBTC_FA_Centroid" type="checkbox" value="true">
//<input name="NBTCGroup.NBTC_FA_Centroid" type="hidden" value="false">

您得到了:

@Html.CheckBoxFor(m => m.NBTCGroup.NBTC_FA_Centroid, nbtcAttr)
//generates:
//<input name="NBTC" data-val="true" data-val-required:"The NBTC_FA_Centroid field is required" id="NBTCGroup_NBTC_FA_Centroid" name="NBTCGroup.NBTC_FA_Centroid" type="checkbox" value="true">
//<input name="NBTCGroup.NBTC_FA_Centroid" type="hidden" value="false">

请注意,在上一个示例中,true和false生成的复选框的名称不匹配.事实证明,用于模型绑定的过程将查找name属性,因此,当我更改名称时,无论是否选中该复选框,它都会从隐藏的输入中获取 false 值.它只是看到正确的名称(而 true 值具有错误的名称)并将该值绑定到模型.

Notice how in the last example, the names of the true and false generated checkboxes mismatch. Turns out the process for model binding looks for the name attribute, thus when I changed the name, it took the false value from the hidden input, no matter if the checkbox was checked or not. It just saw the correct name (while the true value had the incorrect name) and bound that value to the model.

我所做的不是更改名称,而是添加了一个类,并使用它来在我的JS东西中找到我想要的元素:

What I did was instead of changing the name, I just added a class and used that instead in to find the elements I wanted in my JS stuff:

nbtcAttr = new {@class = "NBTC"}

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

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