. NET architecture MVC5 programming for simple shopping websites,. netmvc5

Source: Internet
Author: User

. NET architecture MVC5 programming for simple shopping websites,. netmvc5

1. Create a data model Product first;

Namespace MVCGW. models {public class Product {public int ID {get; set;} [Display (Name = "item Name")] [Required (ErrorMessage = "Required")] [StringLength (60, MinimumLength = 3, ErrorMessage = "must be [3, 60] characters")] public string Title {get; set ;} [Display (Name = "item type")] [Required] public string Genre {get; set;} [Display (Name = "item Introduction")] [Required] public string Description {get; set;} [Display (Name = "")] [Range (1, 10000)] [DataType (DataType. currency)] public decimal Price {get; set;} [Display (Name = "new date")] [DataType (DataType. date)] public DateTime ReleaseDate {get; set;} [Display (Name = "product image")] [Required] public string Photo {get; set ;}}

There are also ShoppingCar (Shopping Cart) and Order (Order) categories,

To save images in the database, the image path is saved;

2. Create a database context class;

public class ProductDBContext : DbContext    {        public DbSet<Product> Products { get; set; }        public DbSet<ShoppingCar> Shoppingcars { get; set; }        public DbSet<Order> Orders { get; set; }    }

3. Create a database connection string;

// Web. config File: <connectionStrings> <add name = "ProductDBContext" connectionString = "Data Source = (LocalDb) \ v11.0; AttachDbFilename = | DataDirectory | \ Products. mdf; AttachDbFilename = | DataDirectory | \ Shoppingcars. mdf; AttachDbFilename = | DataDirectory | \ Orders. mdf; Integrated Security = True "providerName =" System. data. sqlClient "/> </connectionStrings>

4. Add View

The author's website uses layout, which leads to high code utilization.

@ Model IEnumerable <MVCGW. Models. Product> @ {ViewBag. Title = "Index"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}< style>. big {margin: 0px auto 0px auto; width: 940px; height: 516px;/* background-image: url ('/images/tjsp_border.jpg '); /* recommended product background image */padding: 45px 20px 21px 20px;/* The background image box has a certain thickness */}. flower {width: 225px; height: 235px; float: left;/* picture side by side */padding: 12px;/* border: 1px # CCCCCC solid; text-align: center; */overflow: hidden ;}. flower_desc {height: 25px; line-height: 30px; text-align: center; font-size: 12px ;}</style> <p> @ using (Html. beginForm ("Index", "Product", FormMethod. get) {<p> baby type: @ Html. dropDownList ("bbGenre", "all ") <input type = "submit" value = "query"/> </p >}</p> <p class = "big"> @ foreach (var m in Model) {<p class = "flower"> <p>  </p> <p class =" flower_desc "> @ m. title. trim () & nbsp; @ m. price RMB </p> <br/> @ Html. actionLink ("add to shopping cart", "PutCar", new {id = m. ID}) | @ Html. actionLink ("Details", "Details", new {id = m. ID}) </p >}</p>

You can use the Application list template.

5. Create a controller Productcontroller to implement business logic

Compile index,

Public ActionResult Index (string bbGenre) {var GenreLst = new List <string> (); // You can query data by Baby type, var GenreQry = from d in db. products orderby d. genre select d. genre; GenreLst. addRange (GenreQry. distinct (); // deduplicate ViewBag. bbGenre = new SelectList (GenreLst); // The ViewBag attribute value must be consistent with the Select name var rs1 = from m in db. products select m; if (! String. isNullOrEmpty (bbGenre) {rs1 = rs1.Where (x => x. genre = bbGenre); return View (rs1);} var rs = from m in db. products where new int [] {1, 2, 3, 4, 5, 6, 7, 8 }. contains (m. ID) select m; return View (rs); // The data passed to the View is the rs/* ViewData object. model = rs; return View (); */} public ActionResult PutCar (int id) {// The user can only shop after logging on to the instance. if (Session ["Username"] = null) return RedirectToAction ("Login", "Product"); var rec1 = d B. products. find (id); // default quantity sl = 1. You can modify ShoppingCar rec2 = new ShoppingCar {username = (string) Session ["username"], title = rec1.Title, Price = rec1.Price, Genre = rec1.Genre, Photo = rec1.Photo, num = 1}; db. shoppingcars. add (rec2); db. saveChanges (); return RedirectToAction ("Index");} public ActionResult ShoppingCar () {decimal I = 0; var SC = from m in db. shoppingcars select m; if (Session ["Us Ername "]! = Null) {string name = (string) Session ["Username"]; SC = SC. where (s => s. username. contains (name); foreach (var item in SC) {I + = item. price;} ViewBag. data = I; // the back end calculates the total price and transfers the value to the front-end View return View (SC);} else return RedirectToAction ("Login", "Product");} public ActionResult Besure () // confirm the purchase to generate the order, add the purchased item data to the order class, and clear the {var SC = from m in db in the shopping cart. shoppingcars select m; if (Session ["Username"]! = Null) {string name = (string) Session ["Username"]; SC = SC. where (s => s. username = name); foreach (var rec1 in SC) {Order rec2 = new Order {username = (string) Session ["username"], Title = rec1.Title, Price = rec1.Price, genre = rec1.Genre, Photo = rec1.Photo, num = 1}; db. orders. add (rec2);} foreach (var rec3 in SC) {db. shoppingcars. remove (rec3);} db. saveChanges (); return RedirectToAction ("O Rder ");} else return RedirectToAction (" Login "," Product ");} public ActionResult Order () {decimal I = 0; var SC = from m in db. orders select m; if (Session ["Username"]! = Null) {string name = (string) Session ["Username"]; SC = SC. where (s => s. username. contains (name); foreach (var item in SC) {I + = item. price;} ViewBag. data1 = I; return View (SC);} else return RedirectToAction ("Login", "Product ");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.