2016-07-15 35 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

相關問題