2016-04-01 47 views
0

我得到的错误在我的MVC 5应用程序:MVC IPagedList与模型类型的错误

CS1061:“IPagedList”不包含“TargetContact”,没有扩展方法的定义“TargetContact”接受一个类型的第一个参数“IPagedList”可以找到(是否缺少using指令或程序集引用?)

我看到的答案在这里,但我还是不把它做:( 这可能是很容易解决的。

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> 
} 

回答

1

视图中的模型为IPagedList<OutreachSetListViewModel>,所以当你通过模型循环时,每个项目都有一个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> 
} 

控制器

你是不是做与返回的任何有价值的东西从model.ToPagedList(page, 10);

保存它的价值和它传递给视图:

var vm = model.ToPagedList(page, 10); 
return View(vm); 
+0

谢谢!现在它识别属性,但我得到以下错误:传入字典的模型项目类型为'System.Data.Entity.Infrastructure.DbQuery'1 [WebApplication11.Models.OutreachSetListViewModel]',但该字典需要模型项目类型'PagedList.IPagedList'1 [WebApplication11.Models.OutreachSetListViewModel]'。' – user2675973

+1

更新我的回应。 –

+0

就是这样!!!!!谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! – user2675973

0

我知道我迟到了,但是我今天再次解决了这个问题,并通过使用相同的方法解决了问题这是一个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); 
    }