在VIEW试图中经常用到foreach功能,来循环遍历。但经常提示错误,一个重要的原因就是,foreach对泛型或集合的要求。
例如: <ul>
<% foreach (string genreName in Model.Genres) { %>
<li style ="line-height:28px">
<%: Html.ActionLink(genreName, "Browse", new { genre = genreName })%>
</li>
<% } %>
</ul>
这个就要在页面中添加:Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModels.StoreIndexViewModel>" %>
而且在model中StoreIndexViewModel必须有泛型的数据类型。
using System;
using System.Collections.Generic;
namespace MvcMusicStore.ViewModels
{
public class StoreIndexViewModel
{
public int NumberOfGenres { get; set; }
public List<string> Genres { get; set; }
}
}