2013-10-21 38 views
0

我有這樣的代碼在控制器傳遞到詞典中的模型項的類型爲 'System.Collections.Generic.List`1 [Project.Title]'

Expression<Func<Title, bool>> filterExpr = null; 
     Func<IQueryable<Title>, IOrderedQueryable<Title>> orderByFunc = null;   
     List<Title> titles = unitOfWork.TitleRepository.Get(filter: filterExpr, orderBy: orderByFunc, includeProperties: "").ToList();   
     return View("Index", titles); 

這是Model

public partial class Title 
{ 
    public Title() 
    { 
     this.NumberTitles = new HashSet<NumberTitle>(); 
     this.Title1 = new HashSet<Title>(); 
    } 

    public int Id { get; set; } 
    public string TitleText { get; set; } 
    public Nullable<int> TitleId { get; set; } 

    public virtual ICollection<NumberTitle> NumberTitles { get; set; } 
    public virtual ICollection<Title> Title1 { get; set; } 
    public virtual Title Title2 { get; set; } 
} 

,這是查看

@model IEnumerable<CinemavaadabiatModel.Title>  
<table>  
@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.TitleText) 
    </td> 
</tr> 
} 
</table> 

當我運行它,我得到以下錯誤

傳遞到字典中的模型項類型爲'System.Collections.Generic.List 1[Project.Title]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [Project.Title]'。 我的問題在哪裏?

回答

0

在將庫get方法的結果返回給視圖之前,將其轉換爲列表。您可以更改視圖模型的類型List<CinemavaadabiatModel.Title>或更改控制器的動作不調用.ToList()unitOfWork.TitleRepository.Get方法(假設它返回一個IEnumerable):

Expression<Func<Title, bool>> filterExpr = null; 
Func<IQueryable<Title>, IOrderedQueryable<Title>> orderByFunc = null;   
var titles = unitOfWork.TitleRepository.Get(filter: filterExpr, 
    orderBy: orderByFunc, includeProperties: "");   
return View("Index", titles); 

它看起來好像它返回一個IQueryable雖然,所以你最好將域模型的這個IQueryable轉換爲您可以在視圖中使用的視圖模型的IEnumerable,從而將域和表示層關注分開。

更新:

作爲替代方案,你可以創建一個TitleViewModel和標題添加到代替:

public class TitleViewModel 
{ 
    public TitleViewModel() 
    { 
     this.Titles = new List<CinemavaadabiatModel.Title>(); 
    } 

    public IEnumerable<CinemavaadabiatModel.Title> Titles { get; set; } 
} 

然後控制器代碼變爲:

Expression<Func<Title, bool>> filterExpr = null; 
Func<IQueryable<Title>, IOrderedQueryable<Title>> orderByFunc = null;   
var titles = unitOfWork.TitleRepository.Get(filter: filterExpr, 
    orderBy: orderByFunc, includeProperties: ""); 

// consider using some kind of factory to create your ViewModel 
// if unit testing is a concern 
var viewModel = new TitleViewModel { Titles = titles.AsEnumerable() }; 
return View("Index", viewModel); 

而且您的觀點變爲:

@model TitleViewModel 
<table>  
@foreach (var item in Model.Titles) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.TitleText) 
    </td> 
</tr> 
} 
</table> 
+0

通過將IEnumerable 更改爲List 我得到「傳入字典的模型項目類型爲'System.Collections.Generic.List'1 [Project.Title]',但是這個字典需要一個'System.Collections.Generic.List'1 [Project.Title]'「 – nnmmss

+0

類型的模型項目,並且我還將控制器中的行更改爲var titles = unitOfWork.TitleRepository.Get(filter:filterExpr,orderBy: orderByFunc,includeProperties:「」);返回View(titles.AsEnumerable());再次我得到「傳入字典的模型項目的類型'System.Collections.Generic.List'1 [Project.Title]',但這個字典需要一個'System.Collections.Generic.IEnumerable'類型的模型項目[Project.Title]'。 「 – nnmmss

+0

請參閱我的更新 – levelnis

相關問題