c# - Asp.net mvc 不会接受 razor 参数

标签 c# asp.net asp.net-mvc razor

我正在开发一个学校项目,到目前为止进展顺利,但问题终于来了。

我的家庭 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/

相关文章:

c# - 允许您隐式地将 List<ChildClass> 视为 List<ParentClass> 的 C# 功能的名称是什么

C# 日志设计 : Extension method, 的替代方案?

html - Visual Studio 在调试时提取错误的 CSS 文件

asp.net - IdentityServer 3 使用刷新 token 刷新用户

html - 在不使用 Htmlhelpers 的情况下使用单选按钮的 html 映射 ASP.NET MVC 模型

c# - 是否可以将 css 注入(inject)到使用 Html.FromHtml 的 TextView 中?

c# - 使用 HTTPClient 在 foreach 中异步/等待

asp.net - 以编程方式完成 ADFS 登录

javascript - 从 JavaScript 设置组合框所选项目

c# - 将字符串转换或显示为货币