我有這樣的代碼在控制器傳遞到詞典中的模型項的類型爲 '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]'。 我的問題在哪裏?
通過將IEnumerable更改爲List 我得到「傳入字典的模型項目類型爲'System.Collections.Generic.List'1 [Project.Title]',但是這個字典需要一個'System.Collections.Generic.List'1 [Project.Title]'「 –
nnmmss
類型的模型項目,並且我還將控制器中的行更改爲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
請參閱我的更新 – levelnis