我刚刚根据Microsoft提供的教程安装了nuget PagedList,但是我收到以下错误:
“System.InvalidOperationException:传递到字典中的模型项的类型为'System.Collections.Generic.List'1 [BOL4.EventCalendar]',但此字典需要类型为'PagedList.IPagedList'1的模型项[BOL4.EventCalendar]”“。从Add Popup提交新行后。该行将插入数据库中。
这是我的代码:
DentAppEntities db = new DentAppEntities();
[HttpGet]
public ActionResult Add()
{
return PartialView();
}
[HttpPost]
public ActionResult Add(BOL4.EventCalendar appc)
{
if (ModelState.IsValid)
{
db.EventCalendars.Add(appc);
db.SaveChanges();
}
return View("List", db.EventCalendars.ToList());
}
这是我的列表视图:
@model PagedList.IPagedList<BOL4.EventCalendar>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "List";
}
<h2>Home Page</h2>
<p>
<a href="@Url.Action("Add", "Home")" id="Add" class="btn btn-primary"> Add</a>
</p>
<div class="table-responsive">
<table>
<thead>
<tr">
<th>
@Html.ActionLink("Title", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Description", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("StartAt", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("EndAt", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("IsFullDay", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartAt)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndAt)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsFullDay)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("List",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
<div class="modal fade" id="Add-Model" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add Event</h4>
</div>
<div class="divForAdd">
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('#Add').click(function (event) {
event.preventDefault();
$.get(this.href, function (response) {
$('.divForAdd').html(response);
});
$('#Add-Model').modal({
backdrop: 'static',
}, 'show');
return false;
});
});
</script>
这是添加局部视图:
@model BOL4.EventCalendar
@using (Ajax.BeginForm("Add", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "div-record", OnSuccess = "$('.close').click()" }))
{
//Textboxes here
<div class="modal-footer">
<button type="submit" class="btn btn-success" name="cmd">Save</button>
</div>
}
此处还有索引控制器:
public ViewResult List(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var eves = from s in db.EventCalendars
select s;
if (!String.IsNullOrEmpty(searchString))
{
eves = eves.Where(s => s.Title.Contains(searchString)
|| s.Description.Contains(searchString));
}
switch (sortOrder)
{
case "Title":
eves = eves.OrderByDescending(s => s.Title);
break;
case "Description":
eves = eves.OrderBy(s => s.Description);
break;
default: // Name ascending
eves = eves.OrderBy(s => s.Title);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(eves.ToPagedList(pageNumber, pageSize));
}
还尝试了另一种方式,请参阅以下链接:After Submit TotalPages Throws error。任何人都可以帮我解决这个问题。提前致谢!