假設你的類看起來像這樣根據您的意見
public class CourseViewModel {
public int CourseId { get; set; }
public string CourseName { get; set; }
public string CoursePlace { get; set; }
public string CourseLevel { get; set; }
public string CourseDate { get; set; }
public string CourseDescription { get; set; }
public string CourseBookUrl { get; set; }
}
你填入你的列表通過作爲視圖模型爲視圖
public ActionResult Index() {
var viewModel = new List<CourseViewModel>();
//Either get the viewmodels from a data source or add them yourself
var course = new CourseViewModel() {
CourseId = 1,
CourseName = "Maths 101",
CoursePlace = "Some class room",
CourseLevel = "1",
CourseDate = "",
//..populate other properties
};
viewModel.Add(course);
//repeat to add more courses
return View(viewModel);
}
在你看來,你指定什麼類型的視圖模型是
@model List<CourseViewModel>
,你可以遍歷模型,顯示詳細信息。
@foreach (var course in Model) {
<div>@course.CourseName</div>
<!--other details you want to display-->
}
那麼你似乎正在返回一個空列表,並且沒有對你創建的'CourseViewModel'做任何事情,但很難知道你應該做什麼。 –
您遇到的錯誤是什麼?正如@DStanley提到的,你正在返回一個空的ViewModel。 – garfbradaz
@garfbradaz,我的courseviewmodel看起來像這樣的公共虛擬列表 Courses {get;組; } public int CourseId {get;組; } public string CourseName {get;組; } public string CoursePlace {get;組; } public string CourseLevel {get;組; } public string CourseDate {get;組; } public string CourseDescription {get;組; } public string CourseBookUrl {get;組; }然後,我應該如何將actionresult與viewmodel連接起來以顯示課程? –