我正在创建一个Web应用程序以显示一组报告,并且在单击特定报告时,它应该在下面显示该报告的属性。我正在使用两种模型,即Report和Report_Property。 我有这样一种viewModel:
using WebReporting.Models;
using PagedList;
using PagedList.Mvc;
namespace WebReporting.ViewModel
{
public class ReportPropertyIndex
{
public IPagedList<Report> Reports { get; set; }
public IPagedList<Report_Property> Report_Properties { get; set; }
public IEnumerable<Property> Properties { get; set; }
public virtual Flow Flow { get; set; }
public int? RequiredRole { get; set; }
public bool IsGlobal { get; set; }
public int? ShowTreeLevel { get; set; }
}
}
我在Reports_Controller中的索引方法:
using WebReporting.Models;
using PagedList;
using WebReporting.ViewModel;
namespace WebReporting.Controllers
{
public class ReportsController : Controller
{
private DEV_COMPACT db = new DEV_COMPACT();
// GET: Reports
public ActionResult Index(string sortorder,string search,string currentfilter,int? RPPage,int? page,int? reportpage,int? id,int? reportpropertyid)
{
int ReportPageNumber = (reportpage ?? 1);
int RPpageNumber = (RPPage ?? 1);
var viewModel = new ReportPropertyIndex();
viewModel.Reports = db.Reports
.Include(i => i.Report_Property.Select(c => c.Property))
.OrderBy(i => i.ReportName).ToPagedList(ReportPageNumber, 5);
if (id != null)
{
ViewBag.ReportID = id.Value;
viewModel.Report_Properties = viewModel.Reports.Where(
i => i.ReportID == id.Value).Single().Report_Property.ToPagedList(RPpageNumber,10);
}
ViewBag.CurrentSort = sortorder;
ViewBag.UserNameSortParam = String.IsNullOrEmpty(sortorder) ? "report_desc" : "";
ViewBag.ReportNameSortParm = String.IsNullOrEmpty(sortorder) ? "user_desc" : "";
//var reports = db.Reports.Include(r => r.Flow).Include(r => r.User);
if (search != null)
{
page = 1;
}
else
{
search = currentfilter;
}
ViewBag.currentfilter = search;
int pageSize = 5;
int pageNumber = (page ?? 1);
pageNumber = page.HasValue ? Convert.ToInt32(page) : 1;
return View(viewModel);
}
我的索引视图:
@model WebReporting.ViewModel.ReportPropertyIndex
@using PagedList;
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Reports";
}
<style>
</style>
<h2>Report Editor and Viewer</h2>
@using (Html.BeginForm("Index", "Report", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="search" />
</p>
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div class="container" style="font-size:0.9rem;line-height:1.2rem;">
<div class="row">
<div class="col-md-2">
<div class="panel-body">
<table class="table table-hover table-striped table-bordered table-responsive-sm">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Flow.FlowName)
</th>
<th>
@Html.ActionLink("User Name", "Index", new { sortorder = ViewBag.NameSortParm, currentfilter = ViewBag.currenfilter })
</th>
<th>
@Html.ActionLink("Report Name", "Index", new { sortorder = ViewBag.ReportNameSortParm, currentfilter = ViewBag.currentfilter })
</th>
<th>
@Html.DisplayNameFor(model => model.IsGlobal)
</th>
<th>
@Html.DisplayNameFor(model => model.ShowTreeLevel)
</th>
<th>
@Html.DisplayNameFor(model => model.RequiredRole)
</th>
<th style="padding-left:250px"></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Reports)
{
string selectedRow = "";
if (item.ReportID == ViewBag.ReportID)
{
selectedRow = "success";
}
<tr class="@selectedRow">
<td>
@Html.DisplayFor(modelItem => item.Flow.FlowName)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReportName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsGlobal)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShowTreeLevel)
</td>
<td>
@Html.DisplayFor(modelItem => item.RequiredRole)
</td>
<td>
@Html.ActionLink("Select", "Index", new { id = item.ReportID }) |
@Html.ActionLink("Edit", "Edit", new { id = item.ReportID }) |
@Html.ActionLink("Details", "Details", new { id = item.ReportID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ReportID })
</td>
</tr>
}
</tbody>
</table>
在第一页上一切正常,但是当我单击下一页并在该页上选择报告时,就会出现问题,即当我收到错误消息时:System.InvalidOperationException: 'Sequence contains no elements'
罪魁祸首似乎在这里:
if (id != null)
{
ViewBag.ReportID = id.Value;
viewModel.Report_Properties = viewModel.Reports.Where(
i => i.ReportID ==id.Value).Single().Report_Property.ToPagedList(RPpageNumber,10);
}
我还不能弄清楚是什么问题,请帮助!!