该表格不会将数据(产品ID)发送到Razor页面操作方法
[在此处输入图像描述] [1]
生度 [1]: https://i.sstatic.net/6pkfj.png 此事的本质: 有一系列可以添加到购物车中的产品列表,但是当您单击“添加到购物车”时,表单不会传输产品ID,它仅传输零,与此同时找不到此类产品,并且链接到达对象丢失了。
表格的视图:
@model PagedList<Product>
<div class="container-fluid">
@foreach (Product p in Model) {
<div class="row">
<div class="col">
<div class="card m-1 p-1 bg-light">
<div class="bg-faded p-1">
<h4>
@p.Name
<span class="badge badge-pill badge-primary"
style="float:right">
<small>[email protected]</small>
</span>
</h4>
</div>
<form id="@p.Id" asp-page = "/Cart"
method="post">
<input type="hidden" name="Id" value="@p.Id" />
<input type="hidden" name="Name"
value="@p.Name" />
<input type="hidden" name="RetailPrice"
value="@p.RetailPrice" />
<input type="hidden" name="returnUrl" value="@ViewContext.HttpContext.Request.PathAndQuery()" />
<span class="card-text p-1">
@(p.Description
?? "(No Description Available")
<button type="submit"
class="btn btn-success btn-sm pull-right"
style="float:right">
Add To Cart
</button>
</span>
</form>
</div>
</div>
</div>
}
</div>
</div>
</div>
</div>
产品ID应传递给Onpost,但是0通过了,
public class CartModel : PageModel
{
private IRepository repository;
public Cart Cart { get; set; }
public CartModel(IRepository repo, Cart cartService)
{
repository = repo;
Cart = cartService;
}
public string ReturnUrl { get; set; }
public void OnGet(string returnUrl)
{
ReturnUrl = returnUrl ?? "/store";
}
public IActionResult OnPost(long productId, string returnUrl)
{
Product product = repository.Products.FirstOrDefault(p=>p.Id == productId);
Cart.AddItem(product, 1);
return RedirectToPage(new { returnUrl = returnUrl });
}
public IActionResult OnPostRemove(long productId, string returnUrl)
{
Cart.RemoveLine(Cart.Lines.First(c=>c.Product.Id == productId).Product);
return RedirectToPage(new {returnUrl = returnUrl});
}
}
}
我不明白为什么会发生这种情况,我需要您的帮助
[enter image description here][1]
как выглядит ошибка:
[1]: https://i.sstatic.net/6PkFJ.png
The essence of the matter:
there is a list of products that can be added to the cart, but when you click on Add To Cart, the form does not transmit the product Id, it transmits only zero and in the meantime such a product is not found and the link to the object is lost.
View with form:
@model PagedList<Product>
<div class="container-fluid">
@foreach (Product p in Model) {
<div class="row">
<div class="col">
<div class="card m-1 p-1 bg-light">
<div class="bg-faded p-1">
<h4>
@p.Name
<span class="badge badge-pill badge-primary"
style="float:right">
<small>[email protected]</small>
</span>
</h4>
</div>
<form id="@p.Id" asp-page = "/Cart"
method="post">
<input type="hidden" name="Id" value="@p.Id" />
<input type="hidden" name="Name"
value="@p.Name" />
<input type="hidden" name="RetailPrice"
value="@p.RetailPrice" />
<input type="hidden" name="returnUrl" value="@ViewContext.HttpContext.Request.PathAndQuery()" />
<span class="card-text p-1">
@(p.Description
?? "(No Description Available")
<button type="submit"
class="btn btn-success btn-sm pull-right"
style="float:right">
Add To Cart
</button>
</span>
</form>
</div>
</div>
</div>
}
</div>
</div>
</div>
</div>
The Product Id should be passed to the onpost, but 0 is passed there
public class CartModel : PageModel
{
private IRepository repository;
public Cart Cart { get; set; }
public CartModel(IRepository repo, Cart cartService)
{
repository = repo;
Cart = cartService;
}
public string ReturnUrl { get; set; }
public void OnGet(string returnUrl)
{
ReturnUrl = returnUrl ?? "/store";
}
public IActionResult OnPost(long productId, string returnUrl)
{
Product product = repository.Products.FirstOrDefault(p=>p.Id == productId);
Cart.AddItem(product, 1);
return RedirectToPage(new { returnUrl = returnUrl });
}
public IActionResult OnPostRemove(long productId, string returnUrl)
{
Cart.RemoveLine(Cart.Lines.First(c=>c.Product.Id == productId).Product);
return RedirectToPage(new {returnUrl = returnUrl});
}
}
}
I don't understand why this is happening, I need your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输入形式的名称和您在“ Onpost”方法上获得的参数的名称应相互匹配。
而不是此
&lt; input type =“隐藏”名称=“ id” value =“@p.id” /&gt; < /code>
尝试此
&lt; input type =“隐藏”名称=“ productid” value =“@p.id” /&gt; < /code>
the name of the input form and the name of the parameter you get on the "OnPost" method should match each other.
instead of this
<input type="hidden" name="Id" value="@p.Id" />
try this
<input type="hidden" name="productId" value="@p.Id" />
在Razor页面模型绑定中,当您涉及到处理程序方法中添加参数时。这些参数是以表单字段命名的,并为预期数据提供了适当的类型。当发布表单时,剃刀页框架调用了OnPost方法,并看到它具有两个参数。
如果可以将值转换为参数表示的类型,则提取符合参数名称的符合参数名称的表单值,并自动将值分配给参数。
您在OnPost方法中使用
productid
,但是在您的视图中,我看不到您的productid
字段。请检查一下,然后进行一些更改。
In Razor Pages model binding, when you involves adding parameters to the handler method. The parameters are named after the form fields, and given an appropriate type for the expected data. When the form is posted, the Razor Pages framework calls the OnPost method and sees that it has two parameters.
It extracts posted form values that match the names of the parameters and automatically assigns the values from the form to the parameters if the value can be converted to the type represented by the parameter.
You use
productId
in OnPost method, But in your View with form, I cannot see yourproductId
field.Please check it, and make some change .