我在做一個基本的網上商店,學習如何在C#中編程。我是一名初學者,在電子書,YouTube和課程Stackoverflow的幫助下,我學到了一切。現在我遇到了一個我自己無法解決的問題,所以我真的需要你們的幫助。當我選擇它時,ASP.NET MVC不會傳遞ID
問題如下..當我按下菜單中的'webshop'按鈕時,我會進入一個頁面,列出我的所有產品的名稱,圖片,價格等等和'ADD TO CART'按鈕。當我按下這個按鈕時,我應該去我的購物車,我應該看到選定的產品與選定的數量(在這一刻它是默認的1)和產品的價格與所有產品的總價格。當我按下按鈕時,我會去我的購物車,但這個購物車仍然是空的。發送到CartController中我的AddToCart方法的id爲null ...我該如何解決這個問題?你可以看看下面的相關代碼。
RouteConfig
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Webshop
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
ProductSummary(具有AddToCart按鈕局部視圖)
@model Webshop.Models.Entiteiten.Product
<div class="col-md-4">
@if (@Model.ImageData != null)
{
<div class="pull-left" style="margin-right: 10px">
<img class="img-thumbnail" width="75" height="75"
src="@Url.Action("GetImage", "Product",new { @Model.ProductID })" />
</div>
}
<h3>@Model.Name</h3>
@Model.Description
<h4>@Model.Price.ToString("c")</h4>
@using (Html.BeginForm("AddToCart", "Cart"))
{
<div class="pull-right">
@Html.HiddenFor(x => x.ProductID)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)
<input type="submit" class="btn btn-success" value="Add to cart" />
</div>
}
車模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Webshop.Models.Entiteiten
{
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
public void AddItem(Product product, int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine
{
Product = product,
Quantity = quantity
});
}
else {
line.Quantity += quantity;
}
}
public void RemoveLine(Product product)
{
lineCollection.RemoveAll(l => l.Product.ProductID ==
product.ProductID);
}
public decimal ComputeTotalValue()
{
return lineCollection.Sum(e => e.Product.Price * e.Quantity);
}
public void Clear()
{
lineCollection.Clear();
}
public IEnumerable<CartLine> Lines
{
get { return lineCollection; }
}
}
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
}
CartController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webshop.DB.Abstract;
using Webshop.Models.Entiteiten;
using Webshop.Models;
namespace Webshop.Controllers
{
public class CartController : Controller
{
private IProductRepository repository;
public CartController(IProductRepository repo)
{
repository = repo;
}
public ViewResult Index(string returnUrl)
{
return View(new CartIndexViewModel
{
Cart = GetCart(),
ReturnUrl = returnUrl
});
}
public RedirectToRouteResult AddToCart(int? id, string returnUrl)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == id);
if (product != null)
{
GetCart().AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
public RedirectToRouteResult RemoveFromCart(int? id, string returnUrl)
{
Product product = repository.Products
.FirstOrDefault(p => p.ProductID == id);
if (product != null)
{
GetCart().RemoveLine(product);
}
return RedirectToAction("Index", new { returnUrl });
}
private Cart GetCart()
{
Cart cart = (Cart)Session["Cart"];
if (cart == null)
{
cart = new Cart();
Session["Cart"] = cart;
}
return cart;
}
}
}
CartIndexViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Webshop.Models.Entiteiten;
namespace Webshop.Models
{
public class CartIndexViewModel
{
public Cart Cart { get; set; }
public string ReturnUrl { get; set; }
}
}
提前感謝你們!
符合你的行動,我做了第二個解決方案,它完美地工作。非常感謝你,你不能相信我被困在這個問題上多少個小時。再次感謝! – NathanBaele