1.控制器 Controllers/StoreController.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using mvcDemo.Models; // 引入Modelsnamespace mvcDemo.Controllers{ public class StoreController : Controller { // GET: Store public string Index() { return "Hello from Store.Index()"; } // GET: Store public string Browse(string genre) { string message = HttpUtility.HtmlEncode("Genre =" +genre); return message; } // GET: Store public string Details(int id) { string message = "Store.Details,ID = " + id; return message; } public ActionResult List() { var albums = new List(); for (int i = 0;i<10;i++) { albums.Add(new Album { Title = "Album "+ i}); } return View(albums); } }}
2.Models Models/Album.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace mvcDemo.Models{ public class Album { public virtual int AlbumId { get; set; } public virtual int GenreId { get; set; } public virtual int ArtistId { get; set; } public virtual string Title { get; set; } public virtual decimal Price { get; set; } public virtual string AlbumArtUrl { get; set; } public virtual Genre Genre { get; set; } public virtual Artist Artist { get; set; } }}
3.视图层 Views/Store/List.cshtml
@{ ViewBag.Title = "List";}@model IEnumerable@foreach (Album p in Model){
- @p.Title
}
4.配置命名空间 Views/Web.config
点评,这里的传递要比ViewBag更加方便!