ASP.NET MVC 4 Model Class 方法

 
ASP.NET MVC 4 Model Class 方法
 

   /Controllers/HomeController.cs
 
   using System;
   using System.Linq;
   using System.Web.Mvc;
   using MvcApplication2.Models;
 
   namespace MvcApplication.Controllers
   {
      public class HomeController : Controller
      {
         CMSEntities entity;
 
         public ActionResult Index()
         {
            entity = new CMSEntities();
            UserModels u = new MvcApplication2.Models.UserModels();
 
            u.user = entity.Users.Take(10).ToList();
 
            return View(u);
         }
 
         … …
      }
   }
 
   /Models/UserModels.cs
 
   using System.Collections.Generic;
   using System.Web;
 
   namespace MvcApplication.Models
   {
      public class UserModels
      {
         private List<User> _user;
 
         public List<User> user
         {
            set { _user = value; }
            get { return _user; }
         }
      }
   }
 
   /View/Home/Index.cshtml
 
   @model MvcApplication.Models.UserModels
   @{Layout = "";}
   @foreach(var item in Model.user){
      <ul class="round">
         <li>@item.EnglishName</li>
         <li>@item.ChineseName</li>
         <li>@item.Email</li>
         <li>@item.Phone</li>
      </ul>
   }