ASP.NET MVC 4 Controller + View ( Razor ) + Model + DisplayForModel + DisplayTemplates 方法

 
ASP.NET MVC 4 Controller + View ( Razor ) + Model + DisplayForModel + DisplayTemplates 方法
 

   /Controllers/HomeController.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
 
   namespace MvcApplication.Controllers
   {
      public class HomeController : Controller
      {
         CMSEntities entity;
 
         public ActionResult Index()
         {
            entity = new CMSEntities();
            var Model = entity.Users.Take(40);
 
            … …
 
            return View(Model);
         }
      }
   }
 

 

   /Views/Home/Index.cshtml
 
   @Html.DisplayForModel("UserModel")
 

 

   /Views/Shared/DisplayTemplates/UserModel.cshtml
 
   <table border="1" cellpadding="0" cellspacing="0" width="90%">
 
      <tr>
         <th>mail</th>
         <th>English Name</th>
         <th>Chinese Name</th>
         <th>Phone</th>
      </tr>
 
      @foreach (var item in Model)
      {
         <tr>
         @if (item.EnglishName != "" && item.ChineseName != "")
         {
            <td><span>@(item.Email)</span></td>
            <td><span>@(item.EnglishName)</span></td>
            <td><span>@(item.ChineseName)</span></td>
            <td><span>@(item.Phone)</span></td>
         }
         </tr>
      }
 
   </table>