ASP.NET MVC 4 – Dependency Injection ( Unity.Mvc4 ) + AutoMapper + AjaxHelper – ActionLink 方法
( Project : ProductServiceLibrary ) IProductRepository.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProductServiceLibrary { public interface IProductRepository { … … IEnumerable<ProductEntity> GetCategoryProduct(String cat); List<String> GetCategory(); } } |
( Project : ProductServiceLibrary ) ProductRepository.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace ProductServiceLibrary { public class ProductRepository : IProductRepository { private List<ProductEntity> products; private BIProductEntities entity; public ProductRepository() { entity = new BIProductEntities(); products = entity.ProductEntities.OrderBy(x => new { x.cat, x.subcat, x.model }).ToList(); } … … public IEnumerable<ProductEntity> GetCategoryProduct(String cat) { entity = new BIProductEntities(); cat = HttpUtility.UrlDecode(cat); return entity.ProductEntities.Where(c => c.cat == cat).OrderBy(a => new { a.subcat, a.model }).ToList(); } public List<String> GetCategory() { entity = new BIProductEntities(); return entity.ProductEntities.Select(a => a.cat).Distinct().OrderBy(a => a).ToList(); } } } |
( Project : MVCApplication ) \Models\Product.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Globalization; using System.Web.Security; namespace MvcApplication.Models { public class Product { [Key][Required] public int id { get; set; } [Required] public string cat { get; set; } [Required] public string subcat { get; set; } [Required] public string model { get; set; } [Required] public long price { get; set; } } } |
( Project : MVCApplication ) AutoMapperConfig.cs |
using System; using System.Collections.Generic; using AutoMapper; using ProductServiceLibrary; namespace MvcApplication { public class AutoMapperConfig { public static void Configure() { Mapper.CreateMap().ReverseMap(); … … Mapper.AssertConfigurationIsValid(); } } } |
( Project : MvcApplication ) Bootstrapper.cs |
using System.Web.Mvc; using Microsoft.Practices.Unity; using Unity.Mvc4; using ProductServiceLibrary; namespace MvcApplication { public static class Bootstrapper { public static IUnityContainer Initialise() { var container = BuildUnityContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); return container; } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType(); … … container.RegisterType<IProductRepository, ProductRepository>(); return container; } public static void RegisterTypes(IUnityContainer container) { } } } |
( Project : MVCApplication ) Global.asax.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace MvcApplication { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); Bootstrapper.Initialise(); AutoMapperConfig.Configure(); } } } |
( Project : MVCApplication ) \Controllers\HomeController.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ProductServiceLibrary; using AutoMapper; using MvcApplication.Models; namespace MvcApplication.Controllers { public class HomeController : Controller { readonly IProductRepository repository; public HomeController(IProductRepository repository) { this.repository = repository; } … … public ActionResult ProductList() { List<String> list = repository.GetCategory(); return View(list); } [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] public ActionResult ProductCategoryList(String id) { if (id != null && Request.IsAjaxRequest()) { List<Models.Product> list = Mapper.Map(repository.GetCategoryProduct(id.Trim()), new List<Models.Product>()); return View(list); } else { return RedirectToAction("Index"); } } } } |
( Project : MVCApplication ) \Views\Shared\_BlankLayout.cshtml |
@RenderBody() |
( Project : MVCApplication ) \Views\Home\ProductList.cshtml |
@model List<String> <style> .CategoryListItem { font-size: 13px; line-height: 21px; padding-left: 10px; color: #000000; border-bottom: 1px solid #DDDDDD; cursor: pointer; margin: 0px 0px 0px 0px; } .CategoryListItem a { background-color: transparent; text-decoration: none; width: 100%; display: block; } .CategoryListItem:hover a { color: #FFFFFF; } .CategoryListItem:hover { background-color: #442222; } #PageFrameTab, #PageFrameTab tr td, #PageFrameTab tr th { margin-top: 0px !IMPORTANT; padding: 0px 0px 0px 0px !IMPORTANT; height: 21px; font-size: 13px; color: #000000; } #ProductTab tr td { border-bottom: 1px dashed #DDDDDD; color: #000000; font-size: 13px; line-height: 17px; height: 18px; } #ProductTab tr th { border-bottom: 1px solid #888888; color: #000000; font-size: 15px; line-height: 20px; height: 21px; } #ProdPanel { margin: 0px 0px 0px 0px; padding: 0px 0px 0px 0px; display: block; } table { margin-top:0em !IMPORTANT; } </style> <table id="ProductTab" border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td width="20%" valign="top"> @foreach (String item in Model) { <div class="CategoryListItem"> @Ajax.ActionLink(item, "ProductCategoryList", new { id = item }, new AjaxOptions { UpdateTargetId = "ProdPanel", HttpMethod = "GET" }) </div> } </td> <td valign="top"> <div id="ProdPanel"></div> </td> </tr> </table> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } |
( Project : MVCApplication ) \Views\Home\ProductCategoryList.cshtml |
@model List<Models.Product> @{ Layout = "~/Views/Shared/_BlankLayout.cshtml"; } @if (Model != null) { <table id="ProductTab" border="0" cellpadding="0" cellspacing="0" width="100%" style="margin-left:20px;"> <tr> <th valign="top">Subcat</th> <th valign="top">Model</th> <th valign="top">Price</th> </tr> @foreach (Models.Product item in Model) { <tr> <td width="27%" valign="top">( @(item.subcat) )</td> <td valign="top">@(item.model)</td> <td width="15%" valign="top">$@(item.price)</td> </tr> } </table> } |