【问题标题】:Get all Form Collection values where Checkbox checked获取选中复选框的所有表单集合值
【发布时间】:2015-05-26 10:39:44
【问题描述】:

视图如下:

@using (Html.BeginForm("AddCreditLeave", "AdminLeaveCredit"))
{
    <div id="Container">
        <table>
            <tr style="background-color:#A9DDFF;color:Black;">
                <td>Select</td>
                <td>Staff Code</td>
                <td>Name</td>
                <td>Designation</td>
                <td>AL</td>
                <td>CL</td>
            </tr>
            @foreach (var person in Model.Empdetailslist)
            {
                <tr>
                    <td>
                        <input type="checkbox" name="sid" value="@person.staffcode"  id="chk"  />
                    </td>

                    <td>@Html.TextBoxFor(m => person.staffcode, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center" })</td>

                    <td>@Html.TextBoxFor(m => person.name, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center" })</td>

                    <td>@Html.TextBoxFor(m => person.designation, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center" })</td>

                    <td>@Html.TextBoxFor(m => person.ALLeave, new { style = "width:180px; text-align:center" })</td>

                    <td>@Html.TextBoxFor(m => person.CLLeave, new { style = "width:180px; text-align:center" })</td>
                </tr>
            }

        </table>
}

第一列是一个复选框,我显示十行。

第二列是一个文本框。

有一个保存按钮。单击保存按钮时,我想从选中复选框的表单集合中选择第二列的值。

如何做到这一点?

【问题讨论】:

  • 答案可以用jquery吗?
  • 未选中的复选框不会回发,因此回发sid 将包含原始staffcode 值的数组。但是,您的代码没有多大意义,因为您有一个 staffcode 的文本框,但是因为您使用 foreach 循环而不是必需的 for 循环,所以它无论如何都不起作用(并且您有很多无效的 html,因为重复的id 属性)
  • @StephenMuecke 哦。你的意思是我应该使用 For 循环而不是 foreach 并且名称应该像 staffcode[i] ?
  • 您需要的是一个包含(比如说)bool IsSelected 属性的视图模型。然后在视图中使用 for 循环 - for ( int i = 0; i &lt; Model.Count; i++) { @Html.CheckBoxFor(m =&gt; m.IsSelected) ... } 并回发您的视图模型集合(忘记使用 FormCollection
  • @StephenMuecke 虽然我有一些想法,但如果您详细说明并作为答案发布,这将非常有帮助。

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

这就是我将如何通过一个简化的示例来做到这一点。

Working Fiddle

型号

public class AModel
{    
    public string Name { get; set; }            
    public string staffcode { get; set; }           
    public bool Checked { get; set; }          
}

注意Checked 属性

查看循环

@for(var i = 0; i < Model.Empdetailslist.Count; i++)
{
   <tr>
    <td>
        @Html.HiddenFor(m => Model.Empdetailslist[i].Name)
        @Html.HiddenFor(m => Model.Empdetailslist[i].staffcode)
        @Html.CheckBoxFor(m = Model.Empdetailslist[i].Checked)
    </td>
    <td>
        @Model[i].Name
    </td>
  </tr>
}

注意 for 循环而不是 foreach 以启用模型绑定和隐藏字段以允许将值回发到控制器

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

管制员职位

[HttpPost]
public ActionResult AddCreditLeave(YourModel model)
{
   // property will be populated in model.Empdetailslist

    return View(list);
}

【讨论】:

    【解决方案2】:

    像这样编辑您的视图,

    @using (Html.BeginForm("AddCreditLeave", "AdminLeaveCredit"))
    {
        <div id="Container">
            <table>
                <tr style="background-color:#A9DDFF;color:Black;">
                    <td>Select</td>
                    <td>Staff Code</td>
                    <td>Name</td>
                    <td>Designation</td>
                    <td>AL</td>
                    <td>CL</td>
                </tr>
                @foreach (var person in Model.Empdetailslist)
                {
                    <tr>
                        <td>
                            <input type="checkbox" name="sid" value="@person.staffcode"  id="@person.id"  />
                        </td>
    
                        <td>@Html.TextBoxFor(m => person.staffcode, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center", id="staffcode_@person.id" })</td>
    
                        <td>@Html.TextBoxFor(m => person.name, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center", id="name_@person.id" })</td>
    
                        <td>@Html.TextBoxFor(m => person.designation, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center", id="designation_@person.id" })</td>
    
                        <td>@Html.TextBoxFor(m => person.ALLeave, new { style = "width:180px; text-align:center", id="ALLeave_@person.id" })</td>
    
                        <td>@Html.TextBoxFor(m => person.CLLeave, new { style = "width:180px; text-align:center", id="CLLeave_@person.id" })</td>
                    </tr>
                }
    
            </table>
            <input type="button" onclick="SavePersons();" />
    }
    

    使用此代码获取数组中所有选定的人员对象。

    <script type="text/javascript">
    function SavePersons() {
        var chkArr = document.getElementsByName('sid');
        var selectedPersonsArr = new Array();
        for(var i=0; i<chkArr.length; i++) {
            if(chkArr[i].checked == true) {
                var tmpPerson = new Object();
                tmpPerson.id = chkArr[i].id;
                tmpPerson.staffcode = document.getElementById('staffcode'+chkArr[i].id).value;
                tmpPerson.name = document.getElementById('name'+chkArr[i].id).value;
                tmpPerson.designation = document.getElementById('designation'+chkArr[i].id).value;
                tmpPerson.ALLeave = document.getElementById('ALLeave'+chkArr[i].id).value;
                tmpPerson.CLLeave = document.getElementById('CLLeave'+chkArr[i].id).value;
                selectedArr.push(tmpPerson);
            }
        }
        // Now array 'selectedPersonsArr' contains all selected Person objects
        // here you can send these objects to your controller through AJAX
    }
    </script>
    

    希望对你有帮助,谢谢。

    【讨论】:

    • 获得checkbox后,如何获取该checkbox相关列的Form值?
    • 我想在Controller中获取对应的值。
    • 是的。查看代码是否需要更正?当我使用 javascript 选中所有复选框名称时,如何在控制器代码中选择相应的值?
    • 是检查。谢谢。有没有没有javascript代码的方法?就像 Stepehen 建议的那样,它应该返回 ViewModel。
    • 是的,那将是一个非常简单的解决方案,您只需向您的Person 类添加一个布尔型IsSelected 属性并使用@Html.CheckBoxFor(m =&gt; m.IsSelected) 而不是&lt;input type="checkbox" /&gt; 元素。最后,只需将表单提交到您的 [httppost] 操作方法,您将获得更改值的视图模型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多