我一直在尝试从我的数据表中选择多行(使用EF生成),然后将所有选定的行传递到下一个视图以执行某些操作。在将数据传递到下一个视图时,我收到以下错误:
System.NullReferenceException:'对象引用未设置为对象的实例。' 临时的本地类型' int []'>是空的。
如何解决这个问题的任何帮助将不胜感激。
以下是我的代码:
查看:
<div class="row">
<div class="col-md-12">
<!-- Advanced Tables -->
<div class="panel panel-default">
<div class="panel-heading">
@using (Html.BeginForm()) {
<form action="#" method="post">
<label>Search by Company Name:</label> @Html.TextBox("SearchString")
<input type="submit" value="Go" placeholder="Search" style="background-color: #0a9dbd; color: white; border-color: #0a9dbd;">
<label>Search by Card Number:</label> @Html.TextBox("searchCard")
<input type="submit" value="Go" placeholder="Search" style="background-color: #0a9dbd; color: white; border-color: #0a9dbd;">
<a href="ExportToExcel">Export to Excel</a>
</form>
}
</div>
<div class="panel-body">
<a href="@Url.Action(" Create ","CustomerView ")" class="btn btn-primary">Add Gift Card</a>
<a href="@Url.Action(" GetBalance ", "CustomerView ")" class="btn btn-primary">Get Card Balance</a>
<a href="@Url.Action(" PostCards ", "CustomerView ")" class="btn btn-primary">Load Cards</a>
<br />
<br />
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Card ID</th>
<th>Company</th>
<th>Card Number</th>
<th>Card Number 2</th>
<th>Date Created</th>
<th>Card Status</th>
<th>Discount Level ID</th>
<th>Loyalty Level ID</th>
<th>Gift Card Enabled</th>
<th>Loyalty Enabled</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td><input type="checkbox" name="ids" value="@item.CardID" /></td>
<td>@item.CardID</td>
<td>@item.Customer.CustomerCompanyName</td>
<td>@item.CardNumber</td>
<td>@item.CardNumber2</td>
<td>@item.CardDate</td>
<td>@item.CardStatus</td>
<td>@item.DiscountLevelID</td>
<td>@item.LoyaltyLevelID</td>
<td>@item.GiftCardEnabled</td>
<td>@item.LoyaltyEnabled</td>
<td>
<a href="@Url.Action(" Edit ","CustomerView ", new {id = item.CardID} )" class="btn btn-primary"><i class="fa fa-edit "></i> Edit</a> <br />
</td>
</tr>
}
</tbody>
</table>
Page @(Model.PageCount
< Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page=> Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
</div>
</div>
<!--End Advanced Tables -->
</div>
</div>
&#13;
控制器:
public ActionResult PostCards(int[]ids)
{
var myObject = new Card();
foreach(var id in ids)
{
myObject = db.Cards.Single(o => o.CardID == id);
return RedirectToAction("LoadCards", myObject);
}
return View();
}
public ActionResult LoadCards()
{
return View();
}
我需要将选定的数据传递到LoadCards视图。
答案 0 :(得分:0)
让我们先来看看你得到的NullReference。这里的问题是没有创建正确的索引来将复选框绑定到数组。使用for循环而不是foreach。 In MVC/Razor, how do I get the values of multiple checkboxes and pass them all to the controller?
获得所需的行为:
foreach
更改为for
循环,以便创建用于发送数据的正确索引。您的操作应该为每一行收到一组模型。此模型始终传输CardId并告诉我们它是否被选中。
public class SelectedCardModel {
public int CardId { get; set; }
public bool IsSelected {get; set;}
}
在视图中:
@using (Html.BeginForm("PostCards", "CustomerView", FormMethod.Post) {
// <table> etc ...
<tbody>
@for (var i = 0; i < Model.Count; i++) {
@{ var item = Model.ElementAt(i); }
<tr>
<td>
@Html.Hidden("CardId[" + i + "]")
@Html.CheckBox("IsSelected[" + i + "]")
</td>
// display other properties of item ...
<td>@item.CardID</td>
// ...
</tr>
}
</tbody>
</table>
<button type="submit">Load Cards</button>
}
动作:
[HttpPost]
public ActionResult PostCards(SelectedCardModel[] selectedCards) {
foreach(var card in selectedCards) {
if (card.IsSelected) {
var selectedId = card.CardId;
// ...
}
}
}