【问题标题】:Generate URL with multiple input parameters生成具有多个输入参数的 URL
【发布时间】:2018-10-01 18:32:15
【问题描述】:

我想从我的 MVC 应用程序 Razor 视图中生成一个 URL,其中将包含多个参数输入,如下所示:example.com/Index?id=1&bool=true

我已经尝试过这个:@Url.Action("Index?id=1&bool=true", "MyController"),但它根本不起作用。

我想问一下是否有什么想法可以像上面的示例一样获得输出?提前谢谢你。

【问题讨论】:

  • @Url.Action("Index", "ControllerName", new { id = 1, xxx = true })(其中xxx 是您的boolean 属性 - 它不能命名为bool
  • /controllername/index/1?xxx=True 我得到这样的输出。但我想要这样-> Index?id=1&bool=true
  • 如果您的意思是要省略控制器名称,那么除非您编写自定义路由定义,否则您不能这样做

标签: c# asp.net-mvc


【解决方案1】:

您需要更改默认路由RouteConfig.cs

routes.MapRoute(
    name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

routes.MapRoute(
    name: "Default",
                url: "{controller}/{action}",
                defaults: new { controller = "Home", action = "Index" }
            );

然后使用你的URL.Action:

@Url.Action("Index", "ControllerName", new { id = 1, @bool = true })

【讨论】:

    【解决方案2】:

    尝试将输入保存在隐藏字段中,这样当您导航到其他页面时,数据将自动通过查询字符串发送。例如

    @Html.Hidden("Id", Model.Id);

    ID 将在查询字符串中发送

    【讨论】:

    • OP要生成url,提交表单!
    • 感谢您确定,我需要告诉他的是,他应该将项目保存在查询字符串中他想要的隐藏字段中。
    • 没有 OP 生成表单的问题在哪里(这与问题无关)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2014-02-07
    • 2017-10-05
    • 1970-01-01
    • 2015-03-28
    • 2023-04-07
    相关资源
    最近更新 更多