我正在尝试将IEnumerable
传递给我的控制器,以便我可以遍历每一行,进行一些修改并将其添加到数据库中。问题是IEnumberable
在传递回控制器时为Null
。
我已经读过我需要将列表更改为IList
并使用For而不是ForEach - 我理解...但是,现在我的IEnumberable
需要传递给View一个IList
,我不知道该怎么做。
所以,我有几个问题。
IList
是唯一可行的方法,我是否可以将模型作为IEnumerable
传递给控制器?IList
,如何更改当前的AsEnumberable
代码,将其作为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; }
}
答案 0 :(得分:0)
感谢大家的帮助。
我更改了控制器获取如下,我之前尝试用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();
答案 1 :(得分:-1)
我发现这篇文章为我解决了这个问题: https://www.pluralsight.com/guides/asp.net-mvc-getting-default-data-binding-right-for-hierarchical-views
通过循环for-each结构创建视图的方式,创建了一个具有相同ID的行的视图(表结构)。这导致客户端上的数据绑定失败,并且NULL被提交回控制器。
一旦我使用了正确的结构,数据便会正确绑定并发布到接收控制器。
在我上面提供的链接的“保存数据”部分中对此进行了说明。在下面的示例中也有说明。
@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>
}
}