【发布时间】:2017-11-24 16:10:10
【问题描述】:
我正在尝试在 Asp.Net Core 2.0 MVC 中制作一个简单的购物车应用程序。我没有做任何 Ajax-ing。我有三个模型:
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public string Info { get; set; }
public decimal Price { get; set; }
}
public class Cart
{
public int Id { get; set; }
public int CartItemId { get; set; }
public int CustomerId { get; set; } // not in use yet
}
public class CartItem
{
public int Id { get; set; }
public int ProductId { get; set; }
public int NumEach { get; set; } // not in use yet
}
从以下两个视图之一,我想更新 Cart 和 CartItem,然后重定向回我单击“添加到购物车”按钮的视图:
1) 索引视图:
@model IEnumerable<simpleShop.Models.Product>
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.Title)</th>
<th>@Html.DisplayNameFor(model => model.Info)</th>
<th>@Html.DisplayNameFor(model => model.Price)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<a asp-action="Details" asp-route-id="@item.Id">
@Html.DisplayFor(modelItem => item.Title)
</a>
</td>
<td>@Html.DisplayFor(modelItem => item.Info)</td>
<td>@Html.DisplayFor(modelItem => item.Price)</td>
<td>
<form asp-action="AddToCart">
<button type="submit" value="@item.Id">Add to cart</button>
</form>
</td>
</tr>
}
</tbody>
</table>
2) 细节视图:
@model simpleShop.Models.Product
@{
ViewData["Title"] = Html.DisplayFor(model => model.Title);
}
<h2>@Html.DisplayFor(model => model.Title)</h2>
<h4>@Html.DisplayFor(model => model.Info)</h4>
<h1>@Html.DisplayFor(model => model.Price)</h1>
<form asp-action="AddToCart">
<input type="hidden" asp-for="Id" />
<p>
<input type="submit" value="Add to cart" />
</p>
</form>
<div>
<a asp-action="Index">Return to list</a>
</div>
下面是我在家庭控制器中的错误 AddToCart 方法,目前它肯定没有做任何事情来将数据保存到 Cart 或 CartItem 表中。我怎样才能得到它?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddToCart([Bind("Id")] Product product)
{
if (ModelState.IsValid)
{
_context.Add(product);
await _context.SaveChangesAsync();
if (product.Id > 0) // added to cart via the details-view
{
return RedirectToAction("Details", "Home", product.Id);
}
else // added to cart via the index-view
{
return RedirectToAction(nameof(Index));
}
}
return View(product);
}
【问题讨论】:
-
那么目前到底发生了什么?它是否进入 AddToCart 操作?您是否在调试中运行它来检查流程?
-
AddToCart 操作正在运行,但它没有收到任何数据。但就像现在一样,它被设置为写入 Product 表,而不是 Cart 和 CartItem 表。如果我将操作方法更改为 CartItem 而不是 Product,它只会将 NumEach 的 0 和 ProductId 的 0 保存到 CartItem 表,并重定向到没有 Id 的 Home/Details,导致 404。这很奇怪,因为 if -测试product.Id(或cartItem.ID,如果我改变它,没关系)> 0,然后重定向到Home/Details + Id,它应该有一个Id > 0 ...
标签: asp.net-core-mvc asp.net-core-2.0