你好
我的问题是ASP.NET MVC Routing , Html.BeginForm的复制品,
我再次发帖是因为建议的解决方案不起作用。
我的观点:
@using (@Html.BeginForm("Search", "Home",FormMethod.Get))
{
input name="q" id="q" type="text" class="ipt" />
@Html.DropDownList("SearchType", new SelectList(
new[] { "All Words", "Any Word", "ZipCode" }, ("All Words")))
input type="image" src="../../Content/images/search.png" />
}(我删除了<字符,以便在问题中显示)生成的url是这个Home/Search/Brabant/ZipCode,我希望它是http://localhost:4893/Home/Search?q=Brabant&SearchType=ZipCode&x=51&y=5
编辑:
我认为这与路线无关,javascript不起作用!我的问题是首先生成url,而不是匹配它。
$('form').submit(function () {
var data = $('input[name="q"]', this).val();
window.location.href = this.action + '/' + encodeURIComponent(data);
return false;
});发布于 2012-03-20 18:57:23
javascript必须在表单的内
@using (@Html.BeginForm("Search", "Home", FormMethod.Get))
{
<script type="text/javascript">
$('form').submit(function () {
var q = $('input[name="q"]', this).val();
var e = document.getElementById("SearchType");
var SearchType = e.options[e.selectedIndex].text;
var idx = window.location.href.indexOf("/", 7);
var siteName = window.location.href.substring(0, idx).replace("http://", "");
var newPath = "http://" + siteName + '/' + q + '/' + SearchType;
window.location.href = newPath;
return false;
});
</script>
<div class="pf sleft">
<input name="q" id="q" type="text" class="ipt" />
@Html.DropDownList("SearchType", new SelectList(
new[] { "All Words", "Any Word", "ZipCode" }, ("All Words")))
</div>
<div class="pf sright">
<input type="image" onclick="return CheckInput();" src="@Url.Content("~/Content/images/search.png")" />
</div>
}发布于 2012-03-18 11:18:43
在Global.ascx中指定新路由
routes.MapRoute(
"SearchRoute",
"{controller}/{action}/{q}/{SearchType}/{x}/{y}",
new {controller = "Home",action = "Search",q="",SearchType=""},
);编辑:
在您发布的路由之后,您的路由看起来很好,请尝试在默认路由之上定义它们。
发布于 2012-03-19 18:50:10
您收到任何javascript错误吗?您是否签入了firebug或chrome开发工具?
你为什么不把表格交出来
@using (@Html.BeginForm("Search", "Home",FormMethod.Get))
{
input name="q" id="q" type="text" class="ipt" />
@Html.DropDownList("SearchType", new SelectList(
new[] { "All Words", "Any Word", "ZipCode" }, ("All Words")))
<input id="submit" type="image" src="../../Content/images/search.png" />
...^ you had a type here i guess
}和jquery
$("#submit").click(function(e){
$(this).closest('form').submit();
});https://stackoverflow.com/questions/9757844
复制相似问题