【发布时间】:2021-02-14 06:25:05
【问题描述】:
我想将 id 和数量传递给操作,但出现此错误:参数字典包含参数 id 的空条目。
我尝试过地图路由,但找不到正确的方法。
我的行动:
[HttpPost]
public ActionResult Index(int id, int quantity)
{
var user = unitOfWork.Users.FindByEmail(HttpContext.User.Identity.Name);
unitOfWork.Carts.AddProductToCartByEmail(user.Email, id, quantity);
unitOfWork.Complete();
return View();
}
我正在尝试从这个页面传递参数:
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<MVC_Task.Models.AllProductsModel>
@{
ViewBag.Title = "Index";
}
<h2>Products</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().Name)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Price)
</th>
@if (User.Identity.IsAuthenticated)
{
<th>
Quantity
</th>
}
</tr>
@foreach (var item in Model)
{
using (Html.BeginForm("Index", "Product", FormMethod.Post))
{
@Html.AntiForgeryToken()
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
@if (User.Identity.IsAuthenticated)
{
<td class="form-inline">
@Html.TextBoxFor(modelItem => item.Quantity, new { @type = "number", @class = "form-control" })
<input type="submit" value="Add" class="btn btn-default"
onclick="window.location.href = '@Url.Action("Index", new { item.Id, item.Quantity})';" /> //on this line I send the parameters
</td>
}
</tr>
}
}
</table>
<center>@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))</center>
【问题讨论】:
标签: c# asp.net-mvc razor routes