提前感谢您,这就是问题所在:我的表单中有一个Action to Main Controller专门用于索引。第一次进行查询并返回
"http://localhost:59761/Main/Index"
但是当我做第二次下拉选择时,它会返回
"http://localhost:59761/Main/Main/Index"
(返回控制器两次)。基本上我想要的是返回索引。
这是视图:
<form method="post" action="Main/Index"> <select name="week" id="week"> <option value=1>Week 1</option> <option value=2>Week 2</option> <option value=3>Week 3</option> <option value=4>Week 4</option> <option value=5>Week 5</option> </select> <input type="submit" value="Search" id="Search"/> </form>
控制器:
public ActionResult Index(int? week )
{
if (week != null)
{
var quest = (from q in db.weekly_games
where q.week_um == week
select q).ToList();
return View(quest);
}
return View(db.weekly_games.ToList());
}
这是RouteConfig:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }
答案 0 :(得分:1)
使用Html.Beginform
html辅助方法,而不是在表单中对操作值进行硬编码。它将为表单的action
属性呈现正确的值。
@using(Html.BeginForm("Index","Main"))
{
<select name="week" id="week">
<option value=1>Week 1</option>
<option value=2>Week 2</option>
<option value=3>Week 3</option>
<option value=4>Week 4</option>
<option value=5>Week 5</option>
</select>
<input type="submit" value="Search" id="Search"/>
}