C# 使用HttpGet重定向循环MVC3搜索表单
希望有人能帮上忙,因为昨天晚上大部分时间我都在绞尽脑汁!基本上,我正在尝试使用HttpGet获取一个搜索表单,这样我就可以通过URL从外部源检索结果,如: 我创建一个模型并将其传递给我的视图,该模型包含SearchCategory和SearchCriteria的两个属性,并且在视图中具有关联的HTML控件。如果我同时选择了一个类别并为我的标准输入了一些内容,那么这将非常有效。然而,如果我没有在我的条件中输入任何东西,我会得到一个无限重定向。此特定功能的路径如下所示:C# 使用HttpGet重定向循环MVC3搜索表单,c#,asp.net-mvc-3,routes,http-get,C#,Asp.net Mvc 3,Routes,Http Get,希望有人能帮上忙,因为昨天晚上大部分时间我都在绞尽脑汁!基本上,我正在尝试使用HttpGet获取一个搜索表单,这样我就可以通过URL从外部源检索结果,如: 我创建一个模型并将其传递给我的视图,该模型包含SearchCategory和SearchCriteria的两个属性,并且在视图中具有关联的HTML控件。如果我同时选择了一个类别并为我的标准输入了一些内容,那么这将非常有效。然而,如果我没有在我的条件中输入任何东西,我会得到一个无限重定向。此特定功能的路径如下所示: context.MapRo
context.MapRoute(
"Dashboard-Search",
"Area/Controller/Action/{SearchCategory}/{SearchCriteria}",
new {
controller = "Controller",
action = "Action",
SearchCategory = "",
SearchCriteria = ""
}
);
http://example.com/HEP/Dashboard/Search/?Category=SampleCategory&Criteria=SampleCriteria
我确实让我的模型实现了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" />
}
@使用(Html.BeginForm(“搜索”、“仪表板”、FormMethod.Get)){
@LabelFor(m=>m.SearchCategory,“Category:”)
@DropDownListFor(m=>m.SearchCategory,new List()){
新建SelectListItem(){Selected=true,Text=“Category”,Value=“Category”}
})
@LabelFor(m=>m.SearchCriteria,“条件:”)
@Html.TextBoxFor(m=>m.SearchCriteria)
}
您当前的路线与搜索表单不符
满足仪表板搜索路径的URL应该如下所示
http://example.com/HEP/Dashboard/Search/SampleCategory/SampleCriteria
您的表单将依次生成一个GET URL请求,如下所示:
context.MapRoute(
"Dashboard-Search",
"Area/Controller/Action/{SearchCategory}/{SearchCriteria}",
new {
controller = "Controller",
action = "Action",
SearchCategory = "",
SearchCriteria = ""
}
);
http://example.com/HEP/Dashboard/Search/?Category=SampleCategory&Criteria=SampleCriteria
很可能您的方法Search(stringsearchcategory,stringsearchcriteria)
从未被调用过-我建议您的请求被重定向回只显示您的搜索视图而不实际执行任何搜索的方法
如果您需要更多解释或代码,请显示整个注册方法。您现在的路线与您的搜索表单不一致
[HttpGet]
public ActionResult Search(string SearchCategory = null, string SearchCriteria = null)
满足仪表板搜索路径的URL应该如下所示
http://example.com/HEP/Dashboard/Search/SampleCategory/SampleCriteria
您的表单将依次生成一个GET URL请求,如下所示:
context.MapRoute(
"Dashboard-Search",
"Area/Controller/Action/{SearchCategory}/{SearchCriteria}",
new {
controller = "Controller",
action = "Action",
SearchCategory = "",
SearchCriteria = ""
}
);
http://example.com/HEP/Dashboard/Search/?Category=SampleCategory&Criteria=SampleCriteria
很可能您的方法Search(stringsearchcategory,stringsearchcriteria)
从未被调用过-我建议您的请求被重定向回只显示您的搜索视图而不实际执行任何搜索的方法
如果您需要更多解释或代码,请显示整个registerOutes方法
[HttpGet]
public ActionResult Search(string SearchCategory = null, string SearchCriteria = null)
这将允许您的控制器在不使用searchcategory或searchcriteria的情况下工作,只要您处理它们为空的情况
这将允许您的控制器在不使用searchcategory或searchcriteria的情况下工作,只要您处理它们为空的情况。我建议使用一条路由,然后通过使条件可选来设置不同的默认值
context.MapRoute(
"Dashboard-Search",
"HEP/Dashboard/Search/{category}/{criteria}",
new { controller = "Dashboard", action = "Search", category = "Case No", criteria = UrlParameter.Optional });
我建议使用一条路线,然后通过选择标准来设置不同的默认值
context.MapRoute(
"Dashboard-Search",
"HEP/Dashboard/Search/{category}/{criteria}",
new { controller = "Dashboard", action = "Search", category = "Case No", criteria = UrlParameter.Optional });
我知道我可以通过使用客户端验证来防止表单在SearchCriteria中没有任何内容的情况下发布,但我关心的是,如果用户出于某种原因禁用了脚本阻止程序/javascript,会发生什么情况。我认为一个无限循环看起来好像我做得不对!“不要在我的标准中输入任何内容”是什么意思?条件是作为GET参数还是作为URL部分传递的(您的路由代码看起来像后一个选项)?如果条件作为URL的一部分传递,那么不指定它意味着您的URL与仪表板搜索路径的模式不匹配,并且它将通过其他路径重定向。您能否提供更多关于您如何通过标准(示例URL?)的信息,并发布您的整个RegisterRoutes
功能代码?表单中有两个输入框,提交时(带有标准中的内容),键/值对被放入querystring OK中。但是,如果我没有输入任何条件,则只会将类别的键/值对附加到查询字符串。基本上,我想要的是某种防止路由的方法,除非在条件输入中输入了特定的内容。如果您使用的是form method=“get”
,那么您的操作是什么?如果它是空的(未指定),则它必须是当前URL-可以从中访问搜索表单的URL。你能发布它吗?我开始想也许我应该把数据发布到控制器的动作中,然后在那里处理状态检查。如果已发布条件,则使用正确的URL参数重定向到操作,否则会将验证错误返回到视图?我知道我可以通过使用客户端验证来防止表单在SearchCriteria中没有任何内容的情况下发布,但我关心的是,如果用户出于某种原因禁用了脚本阻止程序/javascript,会发生什么情况。我认为一个无限循环看起来好像我做得不对!“不要在我的标准中输入任何内容”是什么意思?条件是作为GET参数还是作为URL部分传递的(您的路由代码看起来像后一个选项)?如果条件作为URL的一部分传递,那么不指定它意味着您的URL与仪表板搜索路径的模式不匹配,并且它将通过其他路径重定向。您能否提供更多关于您如何通过标准(示例URL?)的信息,并发布您的整个RegisterRoutes
功能代码?表单中有两个输入框,提交时(带有标准中的内容),键/值对被放入querystring OK中。但是,如果我没有输入任何条件,则只会将类别的键/值对附加到查询字符串。基本上,我想要的是某种防止路由的方法,除非在条件输入中输入了特定的内容