您有包含照片查看模型列表的頁面的查看模型。此頁面視圖模型包含照片的視圖模型列表。 在查看主網頁呼叫:
@Html.DisplayFor(m => m.PhotosView)
這將使用您定義的默認視圖渲染每個視圖模型。
編輯
class MainPageController
{
ActionResult Index()
{
var model = new MainPageViewModel
{
Photos = GetListOfPhotoViewModelsOrderedByAge(SomeDataSource),
}
return View(model)
}
class MainPageViewModel
{
// various other properties
IList<PhotoViewModels> Photos {get; set;}
}
class PhotoViewModel
{
// properties to display about the photo (including hte path to the actual image)
}
剃刀次(炫魅)
@model MainPageViewModel
@Html.DisplayFor(m =>m.Photos)
@* other things on the page *@
圖片視圖(在共享/顯示目錄)
@model PhotoViewModel
<img url="@Model.PathToImage" />
我還沒有試過這並且它主要來自我的頭頂,可能是輕微的語法錯誤。
這需要我的主頁使用照片模型,雖然,對吧? – 2015-02-12 02:12:06
@JohnShedletsky是的主頁面會有一個照片視圖模型列表。我會用更好的解釋示例更新我的答案。 – THBBFT 2015-02-12 02:19:26