2016-04-01 14 views
0

benim MVC 5 Uygulaması'nda hatayı alıyorum:MVC IPagedList

CS1061: 'IPagedList' 'TargetContact' ve hiçbir uzantı yöntemi için bir tanım içermiyor 'TargetContact' türünde ilk argüman kabul 'IPagedList' bulunamadı (bir kullanarak yönergesi veya bir derleme başvurusu eksik?)

burada cevapları gördüm ama yine de muhtemelen çözmek oldukça kolay :( halletmek yoktur.

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

cevap

1

Görünümdeki model 0'dır., bu nedenle modele göre döngü oluştururken, her öğenin bir TargetContact. Ancak, üstbilgiyi görüntülüyorsanız, DisplayNameFor modelinin öğesi tek öğe değil, liste değildir. Listede TargetContact özelliği yok, bu nedenle listedeki öğelerden birinden almamız gerekiyor.

Bu durumda, listede herhangi bir öğe olup olmadığını denetleyin ve varsa, ilk öğeden TargetContact'u alın.

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

Kontrolör

Bir değere bunun kaydet model.ToPagedList(page, 10);

döndü değerle şey yapıyor ve görünümüne de geçmek değildir:

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

Teşekkür ederiz! Şimdi özellikleri tanır, ancak aşağıdaki hatayı alıyorum '' Sözlüğe aktarılan model öğesi 'System.Data.Entity.Infrastructure.DbQuery'1 [WebApplication11.Models.OutreachSetListViewModel]' türündendir, ancak bu sözlük bir model öğesi gerektirir. 'PagedList.IPagedList'1 türü [WebApplication11.Models.OutreachSetListViewModel]'. ' – user2675973

+1

Yanıtı güncelleştirildi. –

+0

O kadardı !! Teşekkür ederim!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! – user2675973

0

ben geç kaldığımı biliyorum, ama yine de bu problemi bugün aşamalı olarak hallettim ve aynı metodolojiyi kullanarak çözdüm. Bu IEnumerable tüm IEagedList ile IEnumerable yerine yapıldı ve bir çekicilik gibi çalıştı.

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