【发布时间】:2012-01-12 08:22:15
【问题描述】:
希望有人能提供帮助,因为昨天晚上的大部分时间我都在绞尽脑汁!基本上,我正在尝试使用 HttpGet 使搜索表单工作,因此我可以通过以下 URL 从外部源检索结果:
http://url.com/Area/Controller/Action/SearchCategory/SearchCriteria
我创建一个模型并将其传递给我的视图,其中包含 SearchCategory 和 SearchCriteria 的两个属性,并在视图中具有相关的 HTML 控件。如果我同时选择一个类别并为我的标准输入一些内容,这将非常有效。但是,如果我没有在我的标准中输入任何内容,我会得到一个无限重定向。我的这个特定功能的路线如下所示:
context.MapRoute(
"Dashboard-Search",
"Area/Controller/Action/{SearchCategory}/{SearchCriteria}",
new {
controller = "Controller",
action = "Action",
SearchCategory = "",
SearchCriteria = ""
}
);
我确实让我的模型实现了 IValidateableObject 并验证是否输入了某些内容,但显然路由绑定是在验证任何内容之前完成的。
有什么想法吗???
路线
context.MapRoute(
"Dashboard-Search-NoCriteria",
"HEP/Dashboard/Search/{category}",
new { controller = "Dashboard", action = "Search", category = "Case No" }
);
context.MapRoute(
"Dashboard-Search",
"HEP/Dashboard/Search/{category}/{criteria}",
new { controller = "Dashboard", action = "Search", category = "Case No", criteria = "" }
);
控制器动作
[HttpGet]
public ActionResult Search(string SearchCategory, string SearchCriteria)
{
// create new instance of model and add search criteria so entered
// data persists on post back
DashboardModel model = new DashboardModel() {
SearchCategory = SearchCategory,
SearchCriteria = SearchCriteria
};
model.Search(SearchCategory, SearchCriteria);
// return the HTML view of another controller that displays the same list,
// only this time, the list is filtered according to GET data
return View("Overview", model);
}
HTML 表单
@using (Html.BeginForm("Search", "Dashboard", FormMethod.Get)) {
@Html.LabelFor(m => m.SearchCategory, "Category:")
@Html.DropDownListFor(m => m.SearchCategory, new List<SelectListItem>() {
new SelectListItem() { Selected = true, Text = "Category", Value = "Category" }
})
@Html.LabelFor(m => m.SearchCriteria, "Criteria:")
@Html.TextBoxFor(m => m.SearchCriteria)
<input type="submit" value="Search" class="button" />
}
【问题讨论】:
-
我知道我可以通过使用客户端验证来防止这种情况发生由于某种原因被禁用。我认为无限循环看起来就像我做的不对!
-
“不要在我的条件中输入任何内容”是什么意思? Criteria 是作为 GET 参数还是作为 URL 部分传递(您的路由代码看起来像后一个选项)?如果条件作为 URL 的一部分传递,则不指定它意味着您的 URL 与
Dashboard-Search路由的模式不匹配,并且它正在通过其他路由重定向。您能否提供更多关于您如何传递标准的信息(示例 URL?)并发布您的整个RegisterRoutes函数代码? -
在
-
如果您使用的是
form method="get",那么您的action是什么?如果它为空(未指定),则它必须是当前 URL - 您的搜索表单可从中访问的 URL。可以发一下吗? -
我开始认为也许我应该在控制器的操作中发布数据并在那里处理存在检查。如果已发布标准,则 RedirectToAction 使用正确的 URL 参数,否则将验证错误返回到视图?
标签: c# asp.net-mvc-3 routes http-get