Action Method的签名如下所示:
public ActionResult DwellingAdvertsByCity(
string cityName,
int numberOfResultsPerPage,
int pageIndex)
我的Razor表格如下:
@using (Html.BeginForm(
"DwellingAdvertsByCity",
"DwellingAdvert",
new { controller = "DwellingAdvert", action = "DwellingAdvertsByCity" },
FormMethod.Get
))
{
@Html.DropDownList("CityName")
<p>
<input type="hidden" name="numberOfResultsPerPage" id="numberOfResultsPerPage" value="3" />
<input type="hidden" name="pageIndex" id="pageIndex" value="1" />
<input type="submit" value="Submit" />
</p>
}
提交“NewYork”选项后,我会在以下网址下看到结果列表:
http://localhost:XXX/DwellingAdvert/DwellingAdvertsByCity?CityName=NewYork&numberOfResultsPerPage=3&pageIndex=1
任何想法 我可以更改路线配置从基本配置
routes.MapRoute(null, "{controller}/{action}");
要匹配,我会在以下网址下看到结果列表:
http://localhost:XXX/NewYork
?
我花了几个小时试图弄清楚,没有结果,所以我问你们。
有关问题的任何建议吗?
答案 0 :(得分:2)
如果有人对此感兴趣,以下是我发现并为我工作的解决方案:
我在路由配置中添加了以下路由路径,包括 CityName :
routes.MapRoute(null,
"{cityName}", // Matches /NewYork
new { controller = "DwellingAdvert", action = "DwellingAdvertsByCity", pageIndex = 1, numberOfResultsPerPage = 3 }
);
并使用了两个版本的 DwellingAdvertsByCity 操作方法,一个用于其他用于GET的POST请求:
[HttpPost]
[ActionName("DwellingAdvertsByCity")]
public ActionResult DwellingAdvertsByCityPost(
string cityName,
int numberOfResultsPerPage,
int pageIndex)
{
return Redirect(@"~\" + cityName);
}
[HttpGet]
public ActionResult DwellingAdvertsByCity(
string cityName,
int numberOfResultsPerPage,
int pageIndex)
{
... actual code ...
}
所以我可以按照以下网址的预期看到结果:
http://localhost:XXX/NewYork
希望节省您的时间,以防您遇到类似的问题。