mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 16:06:21 +00:00
Ch. 11 SportsStore Admin
This commit is contained in:
parent
9053410db8
commit
5cc0d133ea
@ -10,5 +10,7 @@ namespace SportsStore.Domain.Abstract
|
|||||||
public interface IProductRepository
|
public interface IProductRepository
|
||||||
{
|
{
|
||||||
IEnumerable<Product> Products { get; }
|
IEnumerable<Product> Products { get; }
|
||||||
|
|
||||||
|
void SaveProduct(Product product);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,5 +15,26 @@ namespace SportsStore.Domain.Concrete
|
|||||||
{
|
{
|
||||||
get { return context.Products; }
|
get { return context.Products; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SaveProduct(Product product)
|
||||||
|
{
|
||||||
|
if (product.ProductID == 0)
|
||||||
|
{
|
||||||
|
context.Products.Add(product);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Product dbEntry = context.Products.Find(product.ProductID);
|
||||||
|
if (dbEntry != null)
|
||||||
|
{
|
||||||
|
dbEntry.Name = product.Name;
|
||||||
|
dbEntry.Description = product.Description;
|
||||||
|
dbEntry.Price = product.Price;
|
||||||
|
dbEntry.Category = product.Category;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,13 +2,18 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace SportsStore.Domain.Entities
|
namespace SportsStore.Domain.Entities
|
||||||
{
|
{
|
||||||
public class Product
|
public class Product
|
||||||
{
|
{
|
||||||
|
[HiddenInput(DisplayValue = false)]
|
||||||
public int ProductID { get; set; }
|
public int ProductID { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[DataType(DataType.MultilineText)]
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
public decimal Price { get; set; }
|
public decimal Price { get; set; }
|
||||||
public string Category { get; set; }
|
public string Category { get; set; }
|
||||||
|
47
SportsStore.WebUI/Controllers/AdminController.cs
Normal file
47
SportsStore.WebUI/Controllers/AdminController.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using SportsStore.Domain.Abstract;
|
||||||
|
using SportsStore.Domain.Entities;
|
||||||
|
|
||||||
|
namespace SportsStore.WebUI.Controllers
|
||||||
|
{
|
||||||
|
public class AdminController : Controller
|
||||||
|
{
|
||||||
|
private IProductRepository repository;
|
||||||
|
|
||||||
|
public AdminController(IProductRepository repo)
|
||||||
|
{
|
||||||
|
repository = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionResult Index()
|
||||||
|
{
|
||||||
|
return View(repository.Products);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ViewResult Edit(int productId)
|
||||||
|
{
|
||||||
|
Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
|
||||||
|
|
||||||
|
return View(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public ActionResult Edit(Product product)
|
||||||
|
{
|
||||||
|
if (ModelState.IsValid)
|
||||||
|
{
|
||||||
|
repository.SaveProduct(product);
|
||||||
|
TempData["message"] = string.Format("{0} has been saved", product.Name);
|
||||||
|
return RedirectToAction("Index");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return View(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -136,6 +136,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="App_Start\NinjectWebCommon.cs" />
|
<Compile Include="App_Start\NinjectWebCommon.cs" />
|
||||||
<Compile Include="App_Start\RouteConfig.cs" />
|
<Compile Include="App_Start\RouteConfig.cs" />
|
||||||
|
<Compile Include="Controllers\AdminController.cs" />
|
||||||
<Compile Include="Controllers\CartController.cs" />
|
<Compile Include="Controllers\CartController.cs" />
|
||||||
<Compile Include="Controllers\NavController.cs" />
|
<Compile Include="Controllers\NavController.cs" />
|
||||||
<Compile Include="Controllers\ProductController.cs" />
|
<Compile Include="Controllers\ProductController.cs" />
|
||||||
@ -168,6 +169,9 @@
|
|||||||
<Content Include="Views\Shared\_Layout.Mobile.cshtml" />
|
<Content Include="Views\Shared\_Layout.Mobile.cshtml" />
|
||||||
<Content Include="Views\Nav\FlexMenu.Mobile.cshtml" />
|
<Content Include="Views\Nav\FlexMenu.Mobile.cshtml" />
|
||||||
<Content Include="Views\Shared\ProductSummary.Mobile.cshtml" />
|
<Content Include="Views\Shared\ProductSummary.Mobile.cshtml" />
|
||||||
|
<Content Include="Views\Shared\_AdminLayout.cshtml" />
|
||||||
|
<Content Include="Views\Admin\Index.cshtml" />
|
||||||
|
<Content Include="Views\Admin\Edit.cshtml" />
|
||||||
<None Include="Web.Debug.config">
|
<None Include="Web.Debug.config">
|
||||||
<DependentUpon>Web.config</DependentUpon>
|
<DependentUpon>Web.config</DependentUpon>
|
||||||
</None>
|
</None>
|
||||||
|
49
SportsStore.WebUI/Views/Admin/Edit.cshtml
Normal file
49
SportsStore.WebUI/Views/Admin/Edit.cshtml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
@model SportsStore.Domain.Entities.Product
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "Admin: Edit " + Model.Name;
|
||||||
|
Layout = "~/Views/Shared/_AdminLayout.cshtml";
|
||||||
|
}
|
||||||
|
|
||||||
|
@*<h1>Edit @Model.Name</h1>
|
||||||
|
|
||||||
|
@using (Html.BeginForm())
|
||||||
|
{
|
||||||
|
@Html.EditorForModel()
|
||||||
|
<input type="submit" value="Save" />
|
||||||
|
@Html.ActionLink("Cancel and return to List", "Index")
|
||||||
|
}*@
|
||||||
|
|
||||||
|
<div class="panel">
|
||||||
|
<div class="panel-heading">
|
||||||
|
<h3>Edit @Model.Name</h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@using (Html.BeginForm())
|
||||||
|
{
|
||||||
|
<div class="panel-body">
|
||||||
|
@Html.HiddenFor(m => m.ProductID)
|
||||||
|
@foreach (var property in ViewData.ModelMetadata.Properties)
|
||||||
|
{
|
||||||
|
if (property.PropertyName != "ProeuctID")
|
||||||
|
{
|
||||||
|
<div class="form-group">
|
||||||
|
<label>@(property.DisplayName ?? property.PropertyName)</label>
|
||||||
|
@if (property.PropertyName == "Description")
|
||||||
|
{
|
||||||
|
@Html.TextArea(property.PropertyName, null, new { @class = "form-control", rows = 5 })
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
@Html.TextBox(property.PropertyName, null, new { @class = "form-control" })
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="panel-footer">
|
||||||
|
<input type="submit" value="Save" class="btn btn-primary" />
|
||||||
|
@Html.ActionLink("Cancel and return to List", "Index", null, new { @class = "btn btn-default" } )
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
40
SportsStore.WebUI/Views/Admin/Index.cshtml
Normal file
40
SportsStore.WebUI/Views/Admin/Index.cshtml
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
@model IEnumerable<SportsStore.Domain.Entities.Product>
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "Admin: All Products";
|
||||||
|
Layout = "~/Views/Shared/_AdminLayout.cshtml";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="panel panel-default">
|
||||||
|
<div class="panel-heading">
|
||||||
|
<h3>All Products</h3>
|
||||||
|
</div>
|
||||||
|
<div class="panel-body">
|
||||||
|
<table class="table table-striped table-condensed table-bordered">
|
||||||
|
<tr>
|
||||||
|
<th class="text-right">ID</th>
|
||||||
|
<th>Name</th>
|
||||||
|
<th class="text-right">Price</th>
|
||||||
|
<th class="text-center">Actions</th>
|
||||||
|
</tr>
|
||||||
|
@foreach (var item in Model)
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td class="text-right">@item.ProductID</td>
|
||||||
|
<td>@Html.ActionLink(item.Name, "Edit", new { item.ProductID })</td>
|
||||||
|
<td class="text-right">@item.Price.ToString("c")</td>
|
||||||
|
<td class="text-center">
|
||||||
|
@using (Html.BeginForm("Delete", "Admin"))
|
||||||
|
{
|
||||||
|
@Html.Hidden("ProductID", item.ProductID)
|
||||||
|
<input type="submit" class="btn btn-default btn-xs" value="Delete" />
|
||||||
|
}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div class="panel-footer">
|
||||||
|
@Html.ActionLink("Add a new product", "Create", null, new { @class = "btn btn-default" })
|
||||||
|
</div>
|
||||||
|
</div>
|
24
SportsStore.WebUI/Views/Shared/_AdminLayout.cshtml
Normal file
24
SportsStore.WebUI/Views/Shared/_AdminLayout.cshtml
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
@{
|
||||||
|
Layout = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width" />
|
||||||
|
<link href="~/Content/bootstrap.css" rel="stylesheet" />
|
||||||
|
<link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
|
||||||
|
<link href="~/Content/ErrorStyles.css" rel="stylesheet" />
|
||||||
|
<title>@ViewBag.Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
@if (TempData["message"] != null)
|
||||||
|
{
|
||||||
|
<div class="alert alert-success">@TempData["message"]</div>
|
||||||
|
}
|
||||||
|
@RenderBody()
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user