ASP.NET MVC 5 – Entity Framework 5 ( Insert / Edit / Delete Record ) + View + Model ( EditorTemplates ) 方法

ASP.NET MVC 5 – Entity Framework 5 ( Insert / Edit / Delete Record ) + View + Model ( EditorTemplates ) 方法

   ( Project : SoccerMVCWebApp ) \Controllers\PlayerController.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
   using SoccerMVCDAL.DAL;
   using SoccerMVCDAL;
 
   namespace SoccerMVCWebApp.Controllers
   {
      public class PlayerController : Controller
      {
         PlayerDAL DAL;
 
         public PlayerController()
         {
            DAL = new PlayerDAL();
         }
 
         … …
 
         [HttpPost]
         public ActionResult AddPlayer(PLAYER player)
         {
            if (ModelState.IsValid)
            {
               PLAYER p = new PLAYER();
 
               p.PLAYER_ID = 111111;
               p.PLAYER_NAME = player.PLAYER_NAME;
               p.PLAYER_NATION = 1;
               p.PLAYER_POSITION = 1;
               p.PLAYER_TEAM = 1;
               p.PLAYER_BIRTHDAY = new DateTime();
 
               DAL.Insert<PLAYER>(p);
 
               return RedirectToAction("Index");
            }
            else
            {
               return RedirectToAction("EditForm");
            }
         }
 
         [HttpPost]
         public ActionResult EditPlayer(PLAYER player)
         {
            if (ModelState.IsValid)
            {
               PLAYER p = DAL.getPlayerByPlayerID(player.PLAYER_ID);
               p.PLAYER_NAME = player.PLAYER_NAME;
 
               DAL.Update<PLAYER>(p);
 
               return RedirectToAction("Index");
            }
            else
            {
               return RedirectToAction("EditForm");
            }
         }
 
         public ActionResult Delete(int? ID)
         {
            if(ID.HasValue){
               PLAYER player = DAL.getPlayerByPlayerID(ID.Value);
               if(player != null) DAL.Delete<PLAYER>(player);
            }
 
            return RedirectToAction("Index");
         }
      }
   }
 
   ( Project : SoccerMVCWebApp ) \Views\Player\PlayerForm.cshtml
 
   @model SoccerMVCDAL.PLAYER
 
   @if(Model != null){
 
      <div>Player Edit Form</div>
 
      using (Html.BeginForm("EditPlayer", "Player")) {
         @Html.AntiForgeryToken()
         @Html.ValidationSummary(true)
         @Html.EditorForModel("EditPlayer")
      }
 
   }else{
      <div>Player Create Form</div>
 
      using (Html.BeginForm("AddPlayer", "Player")) {
         @Html.AntiForgeryToken()
         @Html.ValidationSummary(true)
         @Html.EditorForModel("AddPlayer")
      }
 
   }
 
   ( Project : SoccerMVCWebApp ) \Views\Shared\EditorTemplates\AddPlayer.cshtml
 
   @model MVCDAL.PLAYER
 
   <h2>AddPlayer</h2>
 
   <div class="editor-label">
      @Html.LabelFor(model => model.PLAYER_NAME)
   </div>
   <div class="editor-field">
      @Html.TextBoxFor(model => model.PLAYER_NAME, null, new { style = "width:100%;" })
      @Html.ValidationMessageFor(model => model.PLAYER_NAME)
   </div>
 
   <input type="Submit" value="Edit" />
 
   @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
   }
 
   ( Project : SoccerMVCWebApp ) \Views\Shared\EditorTemplates\EditPlayer.cshtml
 
   @model MVCDAL.PLAYER
 
   <h2>Edit Player</h2>
 
   @Html.HiddenFor(model => model.PLAYER_ID)
 
   <div class="editor-label">
      @Html.LabelFor(model => model.PLAYER_NAME)
   </div>
   <div class="editor-field">
      @Html.TextBoxFor(model => model.PLAYER_NAME, null, new { style = "width:100%;" })
      @Html.ValidationMessageFor(model => model.PLAYER_NAME)
   </div>
 
   <input type="Submit" value="Edit" />
 
   @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
   }