ASP.NET MVC 4 ActionLink + URL Routing + 沒有 View + Redirect Action 方法

 
ASP.NET MVC 4 ActionLink + URL Routing + 沒有 View + Redirect Action 方法
 

   /App_Start/RouteConfig.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
   using System.Web.Routing;
 
   namespace MvcApplication
   {
      public class RouteConfig
      {
         public static void RegisterRoutes(RouteCollection routes)
         {
 
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            … …
 
            routes.MapRoute(
               name: "Home",
               url: "Home/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
 
            … …
 
         }
      }
   }
 
   /Home/MVCList ( /Views/Home/MVCList.cshtml )
 
   … …
 
   @foreach (var MVC in Model)
   {
 
      <tr>
 
         <td>
            <span>
               @Html.ActionLink("Delete", "MVCDelete", new { id = MVC.MVC_ID.ToString() })
            </span>
         </td>
 
         … …
 
      </tr>
 
   }
 
   … …
 
   /Controller/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
      {
 
         BITestEntities entity;
 
         … …
 
         public ActionResult MVCDelete(int? id)
         {
            if (id.HasValue)
            {
               entity = new BITestEntities();
 
               MVC DataModel = (entity.MVCs.Where(m => m.MVC_ID == ((int)id)).FirstOrDefault());
 
               if (DataModel != null)
               {
                  entity.MVCs.Remove(DataModel);
                  entity.SaveChanges();
                  return RedirectToAction(“MVCList");
               }
               else
               {
                  return RedirectToAction(“MVCList");
               }
            }
            else
            {
               return RedirectToAction(“MVCList");
            }
         }
 
         … …
 
      }
   }