与往常一样,您从模型开始:
public class MyViewModel{ public int Id { get; set; } public string Title { get; set; }}public class DetailsViewModel{ public string Foo { get; set; } public string Bar { get; set; }}然后是一个控制器:
public class HomeController : Controller{ public ActionResult Index() { // TODO: don't hardpre, fetch from repository var model = Enumerable.Range(1, 10).Select(x => new MyViewModel { Id = x, Title = "item " + x }); return View(model); } public ActionResult Details(int id) { // TODO: don't hardpre, fetch from repository var model = new DetailsViewModel { Foo = "foo detail " + id, Bar = "bar detail " + id }; return PartialView(model); }}和相应的视图。
~/Views/Home/Index.cshtml:
@model IEnumerable<MyViewModel><ul> @Html.DisplayForModel()</ul><div id="details"></div><script type="text/javascript"> $(function () { $('.detailslink').click(function () { $('#details').load(this.href); return false; }); });</script>~/Views/Home/Details.cshtml:
@model DetailsViewModel@Model.Foo@Model.Bar
~/Views/Home/DisplayTemplates/MyViewModel.cshtml:
@model MyViewModel<li> @Html.Actionlink(Model.Title, "details", new { id = Model.Id }, new { @class = "detailslink" })</li>


