简体   繁体   English

根据选中的复选框将值从 MVC 视图传递到 MVC 控制器

[英]pass value from MVC View to MVC Controller depending on checkbox checked

in a MVC C# View I show the records of specifics employees, for that I use MVCScaffolding and the model below在 MVC C# 视图中,我显示了特定员工的记录,为此我使用了 MVCScaffolding 和下面的模型

 public class Developer
    {
        public int code { get; set; }
        public string areaDev { get; set; }
        public string nameDev { get; set; }
        public string expDev { get; set; }
        public string langDev { get; set; }
    }

the view uses razor and for every record there is a checkbox input视图使用剃刀,对于每条记录都有一个复选框输入

@model IEnumerable<WebApplication1.Models.Developer>

@using(Html.BeginForm("ShowRecords","Home"))
{

<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.code)</th>
        <th>@Html.DisplayNameFor(model => model.areaDev)</th>
        <th>@Html.DisplayNameFor(model => model.nameDev)</th>
        <th>@Html.DisplayNameFor(model => model.expDev)</th>
        <th>@Html.DisplayNameFor(model => model.langDev)</th>
        <th>select</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.code)</td>
        <td>@Html.DisplayFor(modelItem => item.areaDev)</td>
        <td>@Html.DisplayFor(modelItem => item.nameDev)</td>
        <td>@Html.DisplayFor(modelItem => item.expDev)</td>
        <td>@Html.DisplayFor(modelItem => item.langDev)</td>
        <td><input type="checkbox" name="code" value="@item.code" /></td>
    </tr>
}
</table>
<input type="submit" value="SEND" />
}

and what I want is to retrieve the information of code(integer code) when the user click the checkbox of one/many specific records displayed in the view,我想要的是当用户单击视图中显示的一个/多个特定记录的复选框时检索代码(整数代码)的信息,

在此处输入图片说明

for that in my controller I receive a int[] as shown below为此,在我的控制器中,我收到一个 int[],如下所示

  public ActionResult ShowRecords(int[] datos)
    {
        {
            foreach(var item in datos)  
            {  ... some code goes here ... }     
            return View(); 
        }

But I don't receive anything from the view, always receive NULL但我没有从视图中收到任何东西,总是收到 NULL

在此处输入图片说明

could you please help me and tell how to retrieve the code info of the due checked row in my controller?你能帮我告诉我如何在我的控制器中检索到期检查行的代码信息吗?

Edit: just added the isChecked property编辑:刚刚添加了 isChecked 属性

public class Developer
    {
        public int code { get; set; }
        public string areaDev { get; set; }
        public string nameDev { get; set; }
        public string expDev { get; set; }
        public string langDev { get; set; }
        public bool isChecked { get; set; }
    }

the controller that sends info to the view has the new property sets to false(in order to not present the checkbox checked)向视图发送信息的控制器将新属性设置为 false(为了不显示复选框)

while (dr.Read())
                {
                    Models.Developer data = new Models.Developer();
                    data.code = Convert.ToInt32(dr["code"]);
                    data.areaDev = dr["areaDev"].ToString();
                    data.nameDev = dr["nameDev"].ToString();
                    data.expDev = dr["expDev"].ToString();
                    data.langDev = dr["langDev"].ToString();
                    data.isChecked = false;
                    Records.Add(data);
                }

in my View I added this在我的视图中我添加了这个

  @Html.CheckBoxFor(modelItem => item.isChecked)

and in the controller I expects to receive a list of developer model在控制器中,我希望收到一份开发人员模型列表

public ActionResult ShowRecords(List<WebApplication1.Models.Developer> datos)

but stills receive NULL但仍然收到 NULL

Your generating checkboxes with name="code" therefore you POST method signature needs to be您生成带有name="code"复选框,因此您需要 POST 方法签名

public ActionResult ShowRecords(int[] code)

The code parameter will contain an array of the values of the checked checkboxes. code参数将包含选中复选框的值的数组。

Based on your edit using a view model, your view will need to use a for loop or custom EditorTemplate so that the name attributes are generated correctly for binding to a collection (refer Post an HTML Table to ADO.NET DataTable for more detail)根据您使用视图模型的编辑,您的视图将需要使用for循环或自定义EditorTemplate以便正确生成name属性以绑定到集合(有关详细信息,请参阅将 HTML 表发布到 ADO.NET DataTable

@for (int i = 0; i < Model.Count; i++)
{
    <td>
        @Html.CheckBoxFor(m => m[i].code) // include hidden input so its submitted
        @Html.DisplayFor(m=> m[i].code)
    </td>
    ....
    <td>@Html.CheckBoxFor(m => m[i].isChecked)</td>
}

and the POST method signature would be并且 POST 方法签名将是

public ActionResult ShowRecords(List<Developer> model)

Change the parameter of your action to IEnumerable<WebApplication1.Models.Developer> and then inspect the isChecked property of each of these (or just use LINQ to filter it down).将您的操作参数更改为IEnumerable<WebApplication1.Models.Developer> ,然后检查每个操作的 isChecked 属性(或仅使用 LINQ 对其进行过滤)。 If you don't like that approach, you could use Javascript to to return those ints.如果您不喜欢这种方法,您可以使用 Javascript 来返回这些整数。 I will point out that returning the model while using more bandwidth (because it's more data going back to the server) but it conforms with the MVC pattern more than returning the ints.我会指出在使用更多带宽的同时返回模型(因为它有更多的数据返回到服务器),但它更符合 MVC 模式而不是返回整数。

You can create a viewModel with a Developper object and an additional isChecked property.您可以使用 Developper 对象和附加的 isChecked 属性创建一个 viewModel。 Then modify your view to use:然后修改您的视图以使用:

IEnumerable <DevelopperViewModel>

as Model for your view and bind the value of isChecked.作为您的视图的模型并绑定 isChecked 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM