【问题标题】:html.beingform not generating appropriate urlhtml.beingform 没有生成适当的 url
【发布时间】:2017-12-28 11:52:02
【问题描述】:

我正在使用 html.beingform 来生成要发布的网址

@using (Html.BeginForm("action", "ctrl", new { id = 5, name = "a" }))

生成的网址是

<form action="/ctrl/action/5?name=a" method="post">

但它应该生成为

<form action="/ctrl/action?name=a&id=5" method="post">

我使用的语法有什么问题??

编辑

我根据 cmets 尝试了以下语法

@using (Html.BeginForm("action", "ctrl", new { [id] = 5, name = "a" }))

上面给了我错误

@using (Html.BeginForm("action", "ctrl", new { @id = 5, name = "a" }))

上面还是把它加到了url

【问题讨论】:

  • 我会先尝试[id] = 5,因为id 对ASP.NET MVC 有特殊意义。
  • 默认路由已经定义了一个可选的id参数,所以它被添加到路由而不是查询字符串中。
  • @AndyG 没用,检查 cmets
  • @Crowcoder 那么如何绕过这个限制呢?
  • 在您的 RegisterRoutes 方法中,您应该调用 MapRoute 来添加它。你可以在那里删除它。

标签: c# asp.net-mvc html.beginform


【解决方案1】:

即使是这样的网址:

 <form action="/ctrl/action/5?name=a" method="post">

该操作确实按预期工作,因为路由定义确实将 5 分配给 id,因此这不是问题。

仍然保持问题的开放性,它可以帮助将来的人

编辑

这是另一种解决方法

  • 定义另一个不带“id”的路由定义
  • 并调用 Html.BeginRouteForm("newRoute", new {id = 5, name = "abcd"})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 2019-08-03
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    相关资源
    最近更新 更多