我正在尝试使用asp.net MVC但是在创建pagedList时遇到了问题。当我的视图使用@model IEnumerable时,视图显示正常,但是当我将其更改为时 @model List或@model pagedList
我在视图中遇到错误 "'列表'不包含'类别1'的定义没有扩展方法' Category1'接受类型' List'的第一个参数。可以找到(你是否错过了使用指令或汇编引用?)"并且每条线都相似。 下面是模型,视图和控制器(仅限索引方法)。
我可能会做一些愚蠢的事,但任何帮助都会受到赞赏。 谢谢,
型号:
namespace IssuesMVC.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Issue
{
public int ID { get; set; }
[Required]
[StringLength(85)]
public string Title { get; set; }
public int? AssignedTo { get; set; }
public int OpenedBy { get; set; }
public DateTime? OpenedDate { get; set; }
public DateTime? DueDate { get; set; }
public DateTime? ClosedDate { get; set; }
public int? Status { get; set; }
public int? Category { get; set; }
public int? Priority { get; set; }
[Column(TypeName = "ntext")]
public string Comment { get; set; }
public virtual Category Category1 { get; set; }
public virtual Contact Contact { get; set; }
public virtual Contact Contact1 { get; set; }
public virtual IssueComment IssueComment { get; set; }
public virtual Priority Priority1 { get; set; }
public virtual Status Status1 { get; set; }
}
}
查看:
@model List<IssuesMVC.Models.Issue>
@using PagedList;
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Issues";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Category1.Category1)
</th>
<th>
@Html.DisplayNameFor(model => model.Contact.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.IssueComment.Comment)
</th>
<th>
@Html.DisplayNameFor(model => model.Priority1.Priority1)
</th>
<th>
@Html.DisplayNameFor(model => model.Status1.Status1)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.OpenedBy)
</th>
<th>
@Html.DisplayNameFor(model => model.OpenedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.DueDate)
</th>
<th>
@Html.DisplayNameFor(model => model.ClosedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category1.Category1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Contact.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IssueComment.Comment)
</td>
<td>
@Html.DisplayFor(modelItem => item.Priority1.Priority1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status1.Status1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Contact1.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OpenedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DueDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClosedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comment)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using issuesMVC.Models;
using PagedList;
namespace IssuesMVC.Controllers
{
public class IssuesController : Controller
{
private IssuesContext db = new IssuesContext();
// GET: Issues
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.OpenedDateParm = String.IsNullOrEmpty(sortOrder) ? "OpenedBy_desc" : "";
ViewBag.OpenedByParm = sortOrder == "OpenedDate" ? "OpenedDate_desc" : "OpenedDate";
page = 1;
var issues = db.issues.Include(i => i.Category1).Include(i => i.Contact).Include(i => i.Contact1).Include(i => i.issueComment).Include(i => i.Priority1).Include(i => i.Status1);
//int pageSize = 3;
int pageNumber = (page ?? 1);
switch (sortOrder)
{
case "OpenedBy_desc":
issues = issues.OrderByDescending(s => s.Contact1.LastName);
breai;
case "OpenedDate":
issues = issues.OrderBy(s => s.OpenedDate);
breai;
case "OpenedDate_desc":
issues = issues.OrderByDescending(s => s.OpenedDate);
breai;
default:
issues = issues.OrderBy(s => s.Contact1.LastName);
breai;
}
return View(issues.ToList());
}
}
}