MVC ASP.NET地图路由无法与表单GET请求一起使用 [英] MVC ASP.NET Map routing is not working with form GET request

查看:87
本文介绍了MVC ASP.NET地图路由无法与表单GET请求一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图中

 @using (Html.BeginForm("PageName","ControllerName", FormMethod.Get))
{
  <input type="hidden" name="categoryName" value="Insurance" />                        
  <input type="hidden" id="cityName6" value="Irvine" name="cityName" />
  <input type="hidden" name="page" value="1" />
  <input type="submit" class="btn btn-default" value="Insurance" />
}

在RouteConfig中-

In RouteConfig-

routes.MapRoute(
                "SomethingRouteName",
                "{categoryName}/{cityName}/{page}",
                new { controller = "ControllerName", action = "PageName" }
);

我希望url像这样显示-Insurance/Irvine/1但是它是这样来的-ControllerName/PageName?categoryName = Insurance& cityName = Irvine& page = 1

I want url to appear like this - Insurance/Irvine/1 But its coming like this- ControllerName/PageName?categoryName=Insurance&cityName=Irvine&page=1

当我使用超链接而不是表单get方法时,此方法很好用.

This works fine when I use hyperlink instead of form get method.

@Html.ActionLink("Insurance", "PageName", "ControllerName", new{ categoryName = "Insurance", cityName = "Irvine", page = 1})

显示

///URL:保险/尔湾/1.但是我必须使用表格GET方法,所以这种超链接方式是没有用的.

//URL shown: Insurance/Irvine/1 as expected. But I have to use form GET method so this hyperlink way is useless.

请帮助

推荐答案

您没有将任何路由值传递给 Html.BeginForm ,因此您呈现的表单元素如下所示:

You're not passing any route values to Html.BeginForm, so your rendered form element looks like this:

<form action="/ControllerName/PageName" method="get">

</form>

因此,当您单击提交时,它只是将表单的值附加为查询字符串.

So when you click submit, it simply appends the values of the form as a query string.

要解决此问题:

@using (Html.BeginForm("PageName", "Home", new {categoryName = "Insurance", cityName = "Irvine", page = "1"}, FormMethod.Get))
{
    <input type="submit" class="btn btn-default" value="Insurance" />
}

这篇关于MVC ASP.NET地图路由无法与表单GET请求一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆