2016-07-15 34 views
0

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

提前感谢你们!

回答

0

这很可能是因为您发布的值与Controller Action期望的参数名称不匹配。如果您看一下现有的表单,则会看到您发布了一个名为ProductID的值给您的Cart/AddToCart控制器动作:

<!-- This will post a value named "ProductID" to Cart/AddToCart --> 
@Html.HiddenFor(x => x.ProductID) 

不过,如果你看看动作本身,它是需要一个参数命名id代替:

public RedirectToRouteResult AddToCart(int? id, string returnUrl) 
{ 
    // Omitted for brevity 
} 

,这是发生的原因是日在HiddenFor()助手将创建与对应于中传递的属性的名称name属性的元素:

<input id='ProductID' name='ProductID' type='hidden' value='{your-product-id}' /> 

当这个被贴了,你AddToCart控制器操作不会看到一个名为参数ProductID,所以它不知道如何映射它(因为只有id在那里)。

可能的解决方法

还有,你能解决这个问题,根据您的喜好了几个possilbe方式。

您AddToCart参数名称重命名从idProductID

public RedirectToRouteResult AddToCart(int? ProductID, string returnUrl) 
{ 
    // Omitted for brevity 
} 

在你的窗体属性重命名从ProductIDid

@Html.Hidden("id", Model.ProductID) 
+0

符合你的行动,我做了第二个解决方案,它完美地工作。非常感谢你,你不能相信我被困在这个问题上多少个小时。再次感谢! – NathanBaele

相关问题