mvc在发布期间维护url参数

时间:2016-05-10 15:13:33

标签: asp.net-mvc

我目前在以下网址上设置了表单:

http://localhost/mySite/inventory/create/26/1

这是一个actionMethod

    [HttpGet]
    public ActionResult create(int exId, int secId)


    [HttpPost]
    public ActionResult create(MyModel model, int exId, int secId, FormCollection form)

表单提交和按钮看起来像:

@using (Html.BeginForm("create", "inventory", new { @exId= Model.ExId, @secId= Model.SecId}, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))

                <div class="col-md-6">
                <input type="submit" class="btn blue" value="Search" name="Searchbtn" id="Searchbtn" />
            </div>

我的问题是在帖子期间仍然保留网址为:/ create / 26/1,现在当帖子发生时,网址被更改为:

http://localhost/mySite/inventory/create?exId=26&secId=1

无论如何都要保持它的获取方式,/ create / 26/1?

1 个答案:

答案 0 :(得分:2)

这很可能是路由问题。 MVC短路路由。换句话说,一旦找到可行的东西,就会使用它,即使可能有更好的路线。在您的场景中,它发现/inventory/create是一个有效的路由,并且由于查询字符串参数是任意的,它只是在那里粘贴其余的路由值。如果没有看到你的路线配置很难说,但是如果/inventory/create/{exId}/{secId}的路线是在/inventory/create捕获的任何路线之后,你应该先移动它。如果您使用属性路由,则没有固有的顺序或路由,因此您必须使用路由名称来区分您真正想要使用的路由名称。

总而言之,这里最简单的方法就是不生成URL。您正在进行回发,因此您可以使用空操作。我认为您主要遇到此问题是因为您尝试将htmlAttributes传递给Html.BeginForm,然后要求您指定一堆额外的内容,而这些内容并非必要。在这些情况下,我建议只使用静态<form>标记。你没有 使用Html.BeginForm当你发现自己做这样的扭曲时,最好不要这样做。

<form class="form-horizontal" role="form" action="" method="post">
    ...
</form>
相关问题