ASP.NET MVC 4 Controller + View ( Razor ) + ActionLink + URL Routing 方法
/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/Form ( /Views/Home/Form.cshtml ) |
<table> @using (@Html.BeginForm("FormResult", "Home")) { <tr> <td>Category :</td> <td>@Html.DropDownList("Category", (SelectList)ViewBag.Category)</td> </tr> <tr> <td colspan="2"> <input type="submit" name="Save" value="Save" /> </td> </tr> } </table> |
/Home/FormResult ( /Views/Home/FormResult.cshtml ) |
<table cellpadding="10" cellspacing="0" width="100%"> @foreach (var product in Model) { <tr> <td> <span> @Html.ActionLink(((int)product.id).ToString(), "Product", new { id = product.id.ToString() }) </span> </td> <td> <span> @product.cat </span> </td> <td> <span> @product.subcat </span> </td> <td> <span> @product.model </span> </td> <td> <span> @product.price </span> </td> </tr> } </table> |
/Home/Product ( /Views/Home/Product.cshtml ) |
@Html.ActionLink("Back to List", "Form") Product<br /><br /> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td><span>ID : </span></td> <td><span>@Model.id</span></td> </tr> <tr> <td><span>Category : </span></td> <td><span>@Model.cat</span></td> </tr> <tr> <td><span>Sub Category : </span></td> <td><span>@Model.subcat</span></td> </tr> <tr> <td><span>Model : </span></td> <td><span>@Model.model</span></td> </tr> <tr> <td><span>Price : </span></td> <td><span>$@Model.price</span></td> </tr> </table> |
/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 Form() { entity = new BITestEntities(); var DataModel = (from Product in entity.ProductEntities select new { Product.cat }).Distinct().ToList(); ViewBag.Category = new SelectList(DataModel, "cat", "cat"); return View(DataModel); } public ActionResult FormResult(FormCollection collection) { entity = new BITestEntities(); String Category = collection["Category"]; if (Category != null) { var DataModel = entity.ProductEntities.Where(r => r.cat == Category).ToList(); return View(DataModel); } else { var DataModel = entity.ProductEntities.ToList(); return View(DataModel); } } public ActionResult Product() { entity = new BITestEntities(); if (RouteData.Values["id"] != null) { int i = 0; String ProductID = RouteData.Values["id"].ToString(); int.TryParse(ProductID, out i); var DataModel = entity.ProductEntities.Where(r => r.id == i).First(); return View(DataModel); } else { var DataModel = entity.ProductEntities.Where(r => r.id == 0).First(); return View(DataModel); } } } } |