将IEnumerable传递给控制器

本文关键字:控制器 IEnumerable | 更新日期: 2023-09-27 18:01:26

我正试图将一个IEnumerable传递给我的控制器,这样我就可以遍历每一行,进行一些修改并将其添加到数据库中。问题是,当IEnumerable被传递回控制器时,它是Null

我读到我需要将我的列表更改为IList,并使用For而不是ForEach——我理解。。。,但是,现在我的IEnumerable需要作为IList传递给View,我不知道该怎么做。

所以,我有几个问题。

  1. IList是唯一的方法吗?我可以不把模型作为IEnumerable传递给控制器吗
  2. 如果必须使用IList,如何更改当前的AsEnumerable代码以将其作为IList发送到视图

以下是在进行任何更改以使其成为IList之前的代码:

获取控制器方法:

public ActionResult Register_step6()
{
    var wp = cg.GetUserWorkPoint();
    var model = (from p in db.WorkPointRoles
                 where p.WorkPoint == wp
                 select p).AsEnumerable()
       .Select(r => new RegisterEmployee_Step6Model()
   {
       Role = r.Role,
       isSelected = false,
       RoleDescription = cg.GetRoleDescription(r.Role),
       IconLocation = cg.GetRoleIconLocation(r.Role)
    });
    return View(model);
}

后控制器方法:

[HttpPost]
public ActionResult Register_step6(IEnumerable<RegisterEmployee_Step6Model> model)
{
    foreach (var i in model)
    {
        //code in here to update db
    }
    db.SaveChanges();
}
        return RedirectToAction("Index");
    }
}

视图:

@model IEnumerable<core_point.Models.RegisterEmployee_Step6Model>
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <h2>Select the roles that you have been employed to do, by clicking the check-box...</h2>
    <table class="table">
        @foreach (var item in Model)
        {
            <tr class="h3">
                <td class="vert-align">
                    <img src="@item.IconLocation" height="80" width="80" />
                </td>
                <td class="vert-align">
                    @Html.CheckBoxFor(modelItem => item.isSelected, new { @class = "mycheckBox" })
                </td>
                <td class="vert-align">
                    @Html.DisplayFor(modelItem => item.RoleDescription)
                </td>
                <td class="vert-align">
                    @Html.TextBoxFor(modelItem => item.Role, new { hidden = "hidden" })
                </td>
            </tr>
        }
    
    </table>
    <div>
        Roles selected:<input type="number" id="noSelected" value="0" />
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Next" class="btn btn-default" />
        </div>
    </div>
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
    var fld_CheckBoxSelected = document.getElementById("CheckBoxSelected");
    var fld_noSelected = document.getElementById("noSelected");
    $(function(){
        $("input.mycheckBox").change(function(){
            var isSelected = this.checked;
            if (isSelected ) {
                $("#noSelected").val( parseInt($("#noSelected").val())+ 1);
            }
            else
            {
                $("#noSelected").val( parseInt($("#noSelected").val()) -  1);
            }
        });
    });
</script>

视图模型类:

public class RegisterEmployee_Step6Model
{
    public byte Id { get; set; }
    public int? Role { get; set; }
    public bool isSelected { get; set; }
    public string RoleDescription { get; set; }
    public string IconLocation { get; set; }
}

将IEnumerable传递给控制器

感谢大家的帮助。

我更改了Controller Get如下,我之前试图用ToList((替换AsEnumerable

        var model = ((from p in db.WorkPointRoles
                     where p.WorkPoint == wp
                     select p).AsEnumerable()
       .Select(r => new RegisterEmployee_Step6Model()
       {
           Role = r.Role,
           isSelected = false,
           RoleDescription = cg.GetRoleDescription(r.Role),
           IconLocation = cg.GetRoleIconLocation(r.Role)
       })).ToList();

我发现这篇文章为我解决了这个问题:https://www.pluralsight.com/guides/asp.net-mvc-getting-default-data-binding-right-for-hierarchical-views

创建视图的方式是,通过循环遍历for each结构,创建具有相同ID的行的视图(表结构(。这导致客户端上的数据绑定失败,NULL被提交回控制器。

一旦我使用了正确的for结构,数据就会正确绑定并发布到接收控制器。

这在";"保存数据";在我上面提供的链接中。下面的示例中也有说明。

@if (Model.Orders != null)
{
    for (var i = 0; i < Model.Orders.Count(); i++)
    {
        <tr>
            @Html.HiddenFor(x => Model.Orders[i].CustomerID)
            <td>
                @Html.TextBoxFor(x => Model.Orders[i].OrderID, new { @class = "form-control", @readonly = "readonly" })
            </td>
            <td>
                @Html.TextBoxFor(x => Model.Orders[i].OrderDate, new { @class = "form-control", @readonly = "readonly" })
            </td>
            <td>
                @Html.TextBoxFor(x => Model.Orders[i].Description, new { @class = "form-control" })
            </td>
        </tr>
    }
}