Ich mache einen einfachen Webshop, um selbst zu lernen, wie man in C# programmiert. Ich bin Anfänger und lerne alles mit Hilfe von E-Books, Youtube und natürlich Stackoverflow. Jetzt bin ich auf ein Problem gestoßen, das ich alleine nicht lösen kann, also brauche ich wirklich die Hilfe von euch.ASP.NET MVC gibt ID nicht weiter, wenn ich es auswähle
Das Problem ist das folgende .. Wenn ich auf den Knopf "Webshop" im Menü drücke, gehe ich zu einer Seite, die alle meine Produkte mit Namen, Bild, Preis usw. usw. und eine Schaltfläche "ADD TO CART" auflistet . Wenn ich diesen Knopf drücke, gehe ich zu meinem Einkaufswagen und ich sollte das ausgewählte Produkt mit der gewählten Menge sehen (in diesem Moment ist es Standard 1) und den Preis des Produktes mit dem Gesamtpreis aller Produkte. Wenn ich den Knopf drücke, gehe ich zu meinem Wagen, aber dieser Wagen bleibt leer. Die ID, die an meine AddToCart-Methode in CartController gesendet wird, ist null ... Wie kann ich das beheben? Sie können sich den entsprechenden Code unten ansehen.
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 (Teilansicht, die die AddToCart Taste hat)
@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>
}
Wagen Modell
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; }
}
}
Vielen Dank im Voraus Jungs!
Ich habe die zweite Lösung und es funktioniert perfekt. Vielen Dank, Sie können nicht glauben, wie viele Stunden ich an diesem Problem festhielt. Danke noch einmal! – NathanBaele