Asp.net 创建页面列表时出现问题
我正在尝试使用asp.net MVC,但在创建页面列表时遇到问题。当我的视图使用@model IEnumerable时,视图显示良好,但当我将其更改为 @型号列表或@model页面列表 我在视图中看到错误 “'List'不包含'Category1'的定义,并且找不到接受'List'类型的第一个参数的扩展方法'Category1'(是否缺少using指令或程序集引用?)”以及每行的类似方法。 模型、视图和控制器(仅索引方法)如下 可能是我在做一些愚蠢的事情,但任何帮助都将不胜感激。 谢谢 型号:Asp.net 创建页面列表时出现问题,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我正在尝试使用asp.net MVC,但在创建页面列表时遇到问题。当我的视图使用@model IEnumerable时,视图显示良好,但当我将其更改为 @型号列表或@model页面列表 我在视图中看到错误 “'List'不包含'Category1'的定义,并且找不到接受'List'类型的第一个参数的扩展方法'Category1'(是否缺少using指令或程序集引用?)”以及每行的类似方法。 模型、视图和控制器(仅索引方法)如下 可能是我在做一些愚蠢的事情,但任何帮助都将不胜感激。 谢谢 型号:
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; }
}
}
视图:
@Html.DisplayNameFor(model=>model.FirstOrDefault().Contact.FirstName)
etc@StephenMuecke谢谢你的回复,这个语法解决了我的问题。你能帮我理解为什么吗?DisplayNameFor()
有两个重载-一个接受TModel
,另一个IEnumerable
-如果不是IEnumerable
(即,声明为@model IEnumerable
则属于TModel
重载-在您的情况下TModel
是List
,而List
仅包含属性Count
等。)
@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());
}
}
}