gpt4 book ai didi

c# - 为什么路由值没有传递给 MVC Controller ?

转载 作者:行者123 更新时间:2023-11-30 13:57:47 24 4
gpt4 key购买 nike

在这里,我在我的 ViewModel 中循环访问 Products:

@foreach (var p in Model.Products)
{
Html.RenderPartial("ProductSummary", p);
<p>@Html.ActionLink("Details...", "Details", "Product", new { p.ProductID }, null)</p>
}

这是我在 ProductController 中的 Details 操作:

public ActionResult Details(int id = 1)
{
return View();
}

id 始终具有默认值 = 1,但我需要选择的 ProductID。我做错了什么?

最佳答案

new { p.ProductID } 将创建一个具有 ProductID 属性的匿名类型。

路由值必须匹配,因此使用 new { id = p.ProductID } 来填充您的 id 参数。

关于c# - 为什么路由值没有传递给 MVC Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com