ASP.NET MVC 4 View ( Razor ) – Controller + Partial View 格式
Controller |
namespace NetworkAssetWeb.Controllers { public class HomeController : Controller { public ActionResult CatItem(string category) { DAO dao = new DAO(); CategoryDatail CatDetail = new CategoryDatail(); … … if (dao.getAuth(System.Web.HttpContext.Current.User.Identity.Name)) { … … ViewBag.ErrorMessage = ""; return View("Item", CatDetail); } else { ViewBag.ErrorMessage = "Access Denied"; return View("Item", null); } } … … public ActionResult LocationCount(string category) { DAO dao = new DAO(); if (category != null) return PartialView(dao.getLocationCount(category)); else return PartialView(dao.getLocationCount("All")); } public ActionResult ItemStatus(string category) { DAO dao = new DAO(); if (category != null) return PartialView(dao.getItemStatus(category)); else return PartialView(dao.getItemStatus("All")); } public ActionResult ItemSubType(string category) { DAO dao = new DAO(); if (category != null) return PartialView(dao.getItemSubType(category)); else return PartialView(dao.getItemSubType("All")); } public ActionResult SystemCount() { DAO dao = new DAO(); return PartialView(dao.getSystemCount()); } public ActionResult ItemMaintenanceDetail(string category) { DAO dao = new DAO(); if (category != null) return PartialView(dao.getInServiceItemWithMain(category)); else return PartialView(dao.getInServiceItemWithMain("All")); } public ActionResult ExistingMaintenanceVendor(string category) { DAO dao = new DAO(); if (category != null) return PartialView(dao.getExistingMaintenanceVendor(category)); else return PartialView(dao.getExistingMaintenanceVendor("All")); } public ActionResult PurchaseYearCount(string category) { DAO dao = new DAO(); if (category != null) return PartialView(dao.getPurchaseYearCount(category)); else return PartialView(dao.getPurchaseYearCount("All")); } } } |
Main View |
@model NetworkAssetDAO.CategoryDatail < div id="topmenu"> < a class="menu" href="/Home/Overall">Overall</a> < a class="menu @(ViewBag.SecuritySelected)" href="/Home/CatItem?Category=Security">Security & Internet</a> < a class="menu @(ViewBag.BackupSelected)" href="/Home/CatItem?Category=Backup">Backup</a> < a class="menu @(ViewBag.NetworkedStorageSelected)" href="/Home/CatItem?Category=Networked Storage">Networked Storage</a> < a class="menu @(ViewBag.SwitchRouterSelected)" href="/Home/CatItem?Category=SwitchRouter">Switch & Router</a> < a class="menu @(ViewBag.ServerSelected)" href="/Home/CatItem?Category=Server">Server</a> < a class="menu @(ViewBag.ToBeCategorizedSelected)" href="/Home/CatItem?Category=To Be Categorized">To Be Categorised</a> < a class="menu @(ViewBag.OthersSelected)" href="/Home/CatItem?Category=Others">Other Infrastructure Hardware</a> < a class="menu" href="/Home/Software">Software</a> @{ if(Model != null) { < a class="Export" href="@(ViewBag.ExportAllCategoryLink)" style="… …">Export to Excel File ( All Categories )</a> < a class="Export" id="ExportExcelIndividual" href="@(ViewBag.ExportLink)" style="… …">Export to Excel File</a> } |
Partial View ( Location Count ) |
@model List<NetworkAssetDAO.LocationCount> < div class="dashboarditem_header"><strong>Item Location Details</strong></div> < table cellspacing="0" cellpadding="0"> <tr> <th valign="middle" align="left">< div>Building</div></th> <th valign="middle" align="right">< div>Count</div></th> </tr> @{ int LocationSum = 0; foreach (var item in Model) { <tr> @if (item.Location == "??" || item.Location.Trim() == "" || item.Location == "Unknown") { <td align="left">< div class="LocationDashBtn">Unknown</div></td> }else{ <td align="left">< div class="LocationDashBtn">@item.Location</div></td> } <td align="right">< div>@item.Count</div></td> </tr> LocationSum += item.Count; } } <tr> <td align="left">< div><strong>Total Item in this Category :</strong></div></td> <td align="right">< div>@(LocationSum)</div></td> </tr> </table> |