我正在开发一个学校项目,到目前为止进展顺利,但问题终于来了。
我的家庭 Controller 中有这个方法:
[HttpPost]
public ActionResult GetSeatsInShow(int showId)
{
...
return View(realSeatList);
}
在我看来,我使用 razor 将参数解析到 Controller , Controller 应该将结果返回到如下地址:
http://localhost:1499/Home/GetSeatsInShow/236
如果我在以下 URL 上名为 Shows 的另一个 Controller 方法上执行此操作,它就会执行此操作:
http://localhost:1499/Home/Shows/1
但在 GetSeatsInShow 方法上,我需要将 ?showId= 放在以下代码片段中:
http://localhost:1499/Home/GetSeatsInShow/?showId=236
我的 Razor ActionLink 看起来像这样:
@Html.ActionLink("Vælg", "GetSeatsInShow", new { id = item.Id })
在 Show 方法正常工作后似乎找不到问题,结果与不起作用的方法相同。
最佳答案
您有几个选择。
您当前将参数作为 id 而不是 showId 传递,而您的 Controller 需要一个名为 showId 的参数。因此,通过更新您的匿名类型,您可以传递正确的参数名称。
@Html.ActionLink("Vælg", "GetSeatsInShow", new { showId = item.Id })
或者,您可以简单地在您的查询字符串中传递“id”,并允许默认的 MVC 绑定(bind)工作,如果您更新您的 Controller ,这很神奇:
@Html.ActionLink("Vælg", "GetSeatsInShow", new { id = item.Id })
[HttpPost]
public ActionResult GetSeatsInShow(int id)
{
...
return View(realSeatList);
}
关于c# - Asp.net mvc 不会接受 razor 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30762976/