2016-04-01 45 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); 
    }