我在做一个基本的网上商店,学习如何在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