C# 将IEnumerable传递给控制器
我试图将IEnumerable传递给我的控制器,这样我就可以遍历每一行,进行一些修改并将其添加到数据库中。问题是IEnumberable在传回控制器时为空 我已经读到,我需要将我的列表更改为IList,并使用For而不是ForEach-我理解…,但是,现在我的IEnumberable需要作为IList传递给视图,我不知道如何做 所以,我有几个问题 IList是唯一的方法吗?我能不能将模型作为IEnumerable传递给控制器? 如果必须使用IList,如何更改当前的AsEnumberable代码以将其作为IList发送到视图? 以下是在进行任何更改以使其成为IList之前的代码: 获取控制器: 邮政总监:C# 将IEnumerable传递给控制器,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我试图将IEnumerable传递给我的控制器,这样我就可以遍历每一行,进行一些修改并将其添加到数据库中。问题是IEnumberable在传回控制器时为空 我已经读到,我需要将我的列表更改为IList,并使用For而不是ForEach-我理解…,但是,现在我的IEnumberable需要作为IList传递给视图,我不知道如何做 所以,我有几个问题 IList是唯一的方法吗?我能不能将模型作为IEnumerable传递给控制器? 如果必须使用IList,如何更改当前的AsEnumberable代码
谢谢大家的帮助 我改变了控制器,如下所示,我以前试图用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();
我发现这篇文章为我解决了这个问题: 视图的创建方式是,通过循环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>
}
}
您不能仅更改控制器方法的签名以接受IList作为您的模型?Jeremy,否接收以下消息:传递到字典的模型项的类型为“System.Linq.Enumerable+Where SelectEnumerableTerator2[core\u point.Models.WorkPointRole,core\u point.Models.RegisterEmployee\u Step6Model]”,但此词典需要类型为“System.Collections.Generic.IList1[core_point.Models.RegisterEmployee_Step6Model]”的模型项。通常,您使用ToArray或ToList而不是AsEnumerable。事实上,除非您真的知道自己在做什么,否则您可能应该远离AsEnumerable。它们都将作为IEnumerable和IList工作,这将更有意义,因为您实际上是在具体化数据。在任何情况下,您都可以在视图中保留IEnumerable,只需确保遵循的MVC规则。如果您将视图中的模型指示器也更改为IList会怎么样?我的视图中有@model IList-是否应更改?这比需要的更复杂。您只需在db.WorkPointRoles中的r中写入var model=from r,其中r.WorkPoint==wp select new registereemployee_Step6Model{Role=r.Role,isSelected=false,RoleDescription=cg.GetRoleDescriptionr.Role,IconLocation=cg.GetRoleIconLocatorr.Role.ToList;.AsEnumerable不做任何事情,您可以用第二个select替换第一个select。
@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; }
}
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();
@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>
}
}