Asp.net mvc 4 MVC IPagedList具有模型类型错误
我的MVC 5应用程序中出现错误: CS1061:“IPagedList”不包含“TargetContact”的定义,并且找不到接受“IPagedList”类型的第一个参数的扩展方法“TargetContact”(是否缺少using指令或程序集引用?) 我在这里看到了答案,但我仍然没有完成:( 这可能很容易解决Asp.net mvc 4 MVC IPagedList具有模型类型错误,asp.net-mvc-4,Asp.net Mvc 4,我的MVC 5应用程序中出现错误: CS1061:“IPagedList”不包含“TargetContact”的定义,并且找不到接受“IPagedList”类型的第一个参数的扩展方法“TargetContact”(是否缺少using指令或程序集引用?) 我在这里看到了答案,但我仍然没有完成:( 这可能很容易解决 public ActionResult Index(string searchTargetContact = null, int page = 1) {
public ActionResult Index(string searchTargetContact = null, int page = 1)
{
var model =
from r in db.Outreach
orderby r.TargetContact descending
where (r.TargetContact.StartsWith(searchTargetContact) || searchTargetContact == null)
select new Models.OutreachSetListViewModel
{
TargetContact = r.TargetContact,
NextOutreachStep = r.NextOutreachStep,
GoalOfOutreach = r.GoalOfOutreach,
};
model.ToPagedList(page, 10);
return View(model);
namespace WebApplication11.Models
{
public class OutreachSetListViewModel
{
public string NextOutreachStep { get; set; }
public string TargetContact { get; set; }
public string GoalOfOutreach { get; set; }
}
}
@model IPagedList<OutreachSetListViewModel>
<table class="table" id="networkingList">
<tr>
<th>@Html.DisplayNameFor(model => model.TargetContact)</th>
<th>@Html.DisplayNameFor(model => model.NextOutreachStep)</th>
<th>@Html.DisplayNameFor(model => model.GoalOfOutreach)</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.TargetContact)</td>
<td>@Html.DisplayFor(modelItem => item.NextOutreachStep)</td>
<td>@Html.DisplayFor(modelItem => item.GoalOfOutreach)</td>
</tr>
}
公共操作结果索引(字符串searchTargetContact=null,int page=1)
{
var模型=
从数据库中的r开始
orderby r.TargetContact降序
其中(r.TargetContact.StartsWith(searchTargetContact)| | searchTargetContact==null)
选择新模型。OutreachSetListViewModel
{
TargetContact=r.TargetContact,
NextOutreachStep=r.NextOutreachStep,
GoalOfOutreach=r.GoalOfOutreach,
};
型号.ToPagedList(第10页);
返回视图(模型);
名称空间WebApplication11.Models
{
公共类OutreachSetListViewModel
{
公共字符串NextOutreachStep{get;set;}
公共字符串TargetContact{get;set;}
公共字符串GoalOfOutreach{get;set;}
}
}
@IPagedList模型
@DisplayNameFor(model=>model.TargetContact)
@DisplayNameFor(model=>model.NextOutreachStep)
@DisplayNameFor(model=>model.GoalOfOutreach)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.TargetContact)
@DisplayFor(modelItem=>item.NextOutreachStep)
@DisplayFor(modelItem=>item.GoalOfOutreach)
}
视图中的模型是
IPagedList
,因此当您在模型中循环时,每个项目都有一个TargetContact
但是,当您显示标题时,DisplayNameFor
的模型不是单个项目,而是列表。该列表没有TargetContact
属性,因此我们必须从列表中的一个项目获取它
在本例中,我们检查列表中是否有任何元素,如果有,从第一个元素获取TargetContact
@if(Model.Any())
{
<tr>
<th>@Html.DisplayNameFor(model => model[0].TargetContact)</th>
<th>@Html.DisplayNameFor(model => model[0].NextOutreachStep)</th>
<th>@Html.DisplayNameFor(model => model[0].GoalOfOutreach)</th>
<th></th>
</tr>
}
视图中的模型是
IPagedList
,因此当您在模型中循环时,每个项目都有一个TargetContact
但是,当您显示标题时,DisplayNameFor
的模型不是单个项目,而是列表。该列表没有TargetContact
属性,因此我们必须从列表中的一个项目获取它
在本例中,我们检查列表中是否有任何元素,如果有,从第一个元素获取TargetContact
@if(Model.Any())
{
<tr>
<th>@Html.DisplayNameFor(model => model[0].TargetContact)</th>
<th>@Html.DisplayNameFor(model => model[0].NextOutreachStep)</th>
<th>@Html.DisplayNameFor(model => model[0].GoalOfOutreach)</th>
<th></th>
</tr>
}
我知道我迟到了,但我今天再次将这个问题分阶段解决,并使用IEnumerable时使用的相同方法解决了这个问题。我所做的就是用IPagedList替换IEnumerable,它工作起来很有魅力
public static string DisplayNameFor<TModelItem, TResult>(this IHtmlHelper<IEnumerable<TModelItem>> htmlHelper, Expression<Func<TModelItem, TResult>> expression)
{
if (htmlHelper == null)
throw new ArgumentNullException(nameof(htmlHelper));
if (expression == null)
throw new ArgumentNullException(nameof(expression));
return htmlHelper.DisplayNameForInnerType(expression);
}
public static string DisplayNameFor<TModelItem, TResult>(this IHtmlHelper<IPagedList<TModelItem>> htmlHelper, Expression<Func<TModelItem, TResult>> expression)
{
if (htmlHelper == null)
throw new ArgumentNullException(nameof(htmlHelper));
if (expression == null)
throw new ArgumentNullException(nameof(expression));
return htmlHelper.DisplayNameForInnerType(expression);
}
public静态字符串DisplayNameFor(此IHtmlHelper htmlHelper,表达式)
{
如果(htmlHelper==null)
抛出新ArgumentNullException(nameof(htmlHelper));
if(表达式==null)
抛出新ArgumentNullException(nameof(expression));
返回htmlHelper.DisplayNameForInnerType(表达式);
}
公共静态字符串DisplayNameFor(此IHtmlHelper htmlHelper,表达式)
{
如果(htmlHelper==null)
抛出新ArgumentNullException(nameof(htmlHelper));
if(表达式==null)
抛出新ArgumentNullException(nameof(expression));
返回htmlHelper.DisplayNameForInnerType(表达式);
}
我知道我迟到了,但我今天再次将这个问题分阶段解决,并使用与IEnumerable相同的方法解决了问题。我所做的就是用IPagedList替换IEnumerable,效果非常好
public static string DisplayNameFor<TModelItem, TResult>(this IHtmlHelper<IEnumerable<TModelItem>> htmlHelper, Expression<Func<TModelItem, TResult>> expression)
{
if (htmlHelper == null)
throw new ArgumentNullException(nameof(htmlHelper));
if (expression == null)
throw new ArgumentNullException(nameof(expression));
return htmlHelper.DisplayNameForInnerType(expression);
}
public static string DisplayNameFor<TModelItem, TResult>(this IHtmlHelper<IPagedList<TModelItem>> htmlHelper, Expression<Func<TModelItem, TResult>> expression)
{
if (htmlHelper == null)
throw new ArgumentNullException(nameof(htmlHelper));
if (expression == null)
throw new ArgumentNullException(nameof(expression));
return htmlHelper.DisplayNameForInnerType(expression);
}
public静态字符串DisplayNameFor(此IHtmlHelper htmlHelper,表达式)
{
如果(htmlHelper==null)
抛出新ArgumentNullException(nameof(htmlHelper));
if(表达式==null)
抛出新ArgumentNullException(nameof(expression));
返回htmlHelper.DisplayNameForInnerType(表达式);
}
公共静态字符串DisplayNameFor(此IHtmlHelper htmlHelper,表达式)
{
如果(htmlHelper==null)
抛出新ArgumentNullException(nameof(htmlHelper));
if(表达式==null)
抛出新ArgumentNullException(nameof(expression));
返回htmlHelper.DisplayNameForInnerType(表达式);
}
谢谢!现在它可以识别属性,但我得到以下错误传递到字典中的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery'1[WebApplication11.Models.OutreachSetListViewModel]”,但此字典需要“PagedList.IPagedList”类型的模型项[WebApplication11.Models.OutreachSetListViewModel]。`更新了我的响应。谢谢!现在它可以识别属性,但我得到以下错误传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery'1[WebApplication11.Models.OutreachSetListViewModel]“,但此词典需要类型为“PagedList.IPagedList
1[WebApplication11.Models.OutreachSetListViewModel]”的模型项。`已更新我的响应。