MVC美食店添加购物车功能异常及'id当前上下文不存在'报错求助
Hey there! Let’s work through this frustrating error you’re seeing when building the "Add to Cart" functionality. This "id not found" issue is super common in MVC apps, and it almost always traces back to a scope or parameter-passing problem—let’s break down the most likely fixes:
1. Check Your Controller Method’s Parameter Declaration
First things first: make sure your "Add to Cart" action method actually accepts an id parameter. If you’re trying to use id in the method but haven’t defined it in the method signature, the compiler won’t recognize it. For example:
// Correct: id is declared as a method parameter public ActionResult AddToCart(int id) { // Now you can use 'id' here to fetch the product var product = YourProductRepository.GetProductById(id); // Rest of your cart logic... return RedirectToAction("Cart"); }
If you’re using a different parameter name (like productId), you’ll need to reference that exact name instead of id throughout the method.
2. Verify the Request is Passing the ID Correctly
Even if your controller accepts id, if the request from your view doesn’t send the product ID, the parameter will be undefined. Let’s check your view code:
- If using an action link:
<!-- Make sure you're passing the product ID in the route values --> @Html.ActionLink("添加至购物车", "AddToCart", "Order", new { id = Model.Id }, null) - If using a form submission:
<form action="@Url.Action("AddToCart", "Order")" method="post"> <!-- Add a hidden field to pass the product ID --> <input type="hidden" name="id" value="@Model.Id" /> <button type="submit">添加至购物车</button> </form>
Double-check that Model.Id actually holds the correct product ID in your view—you can debug this by adding @Model.Id directly in the view to see if it renders a valid number.
3. Check Your Product Retrieval Class
You mentioned you have a class that fetches products from the database. Make sure when you call that class’s method, you’re passing the id correctly (and that the id exists in the current scope when you make the call). For example:
// Example product retrieval class method public class ProductRepository { public Product GetProductById(int productId) { using (var db = new YourDbContext()) { return db.Products.FirstOrDefault(p => p.Id == productId); } } } // In your controller method public ActionResult AddToCart(int id) { var repo = new ProductRepository(); // Make sure you're passing the 'id' parameter here, not an undefined variable var product = repo.GetProductById(id); // Cart logic... }
4. Watch for Case Sensitivity
C# is case-sensitive! If your model defines the property as Id (uppercase I), but you accidentally reference id (lowercase) somewhere that’s not a parameter, that could cause confusion. Though your error specifically says the name doesn’t exist, it’s worth a quick check to rule out typos.
Quick Debugging Tip
Set a breakpoint at the line where the error occurs. When the debugger hits it, hover over variables to see which ones are in scope. If id isn’t listed, that confirms it’s not being declared or passed correctly to that part of the code.
内容的提问来源于stack exchange,提问作者Waled.Nur74

