Asp.net mvc 3 将Html.BeginForm映射到路由
您好Asp.net mvc 3 将Html.BeginForm映射到路由,asp.net-mvc-3,asp.net-mvc-routing,Asp.net Mvc 3,Asp.net Mvc Routing,您好我的问题是,我再次发帖,因为建议的解决方案不起作用。我的观点: @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",
我的问题是,
我再次发帖,因为建议的解决方案不起作用。
我的观点:
@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" />
}
(我删除了编辑: 我认为这与路由无关,javascript不起作用!我的问题是首先生成url,而不是匹配它
$('form').submit(function () {
var data = $('input[name="q"]', this).val();
window.location.href = this.action + '/' + encodeURIComponent(data);
return false;
});
在Global.ascx中指定新路由
routes.MapRoute(
"SearchRoute",
"{controller}/{action}/{q}/{SearchType}/{x}/{y}",
new {controller = "Home",action = "Search",q="",SearchType=""},
);
编辑:
发布路由后,您的路由看起来很好,请尝试在默认路由上方定义它们在Global.ascx中指定新路由
routes.MapRoute(
"SearchRoute",
"{controller}/{action}/{q}/{SearchType}/{x}/{y}",
new {controller = "Home",action = "Search",q="",SearchType=""},
);
编辑:
发布路由后,您的路由看起来很好,请尝试在默认路由上方定义它们是否有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
}
您是否有任何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
}
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>
}
@使用(@Html.BeginForm(“搜索”,“主页”,FormMethod.Get))
{
$('form')。提交(函数(){
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;
返回false;
});
@Html.DropDownList(“SearchType”),新的选择列表(
新[]{“所有单词”、“任何单词”、“ZipCode”},(“所有单词”))
}
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>
}
@使用(@Html.BeginForm(“搜索”,“主页”,FormMethod.Get))
{
$('form')。提交(函数(){
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;
返回false;
});
@Html.DropDownList(“SearchType”),新的选择列表(
新[]{“所有单词”、“任何单词”、“ZipCode”},(“所有单词”))
}
我认为我们需要查看路由的代码(在global.asax中)来帮助.routes.MapRoute(null,//路由名称“”///URL,参数为new{controller=“Home”,action=“Index”});MapRoute(null,“Home/Search/{q}/{SearchType}/{x}/{y}”、new{controller=“Home”、action=“Search”}、//默认值new{page=@“\d+”}//约束:页面必须是数字);MapRoute(null,{q}/{SearchType}/Page{Page},new{controller=“Home”,action=“Search”},//默认值new{Page=@“\d+”}//约束:页面必须是数字);MapRoute(null,“{controller}/{action}”);我相信我们需要查看路由的代码(在global.asax中)来帮助.routes.MapRoute(null,//路由名称“”//URL,参数为new{controller=“Home”,action=“Index”});MapRoute(null,“Home/Search/{q}/{SearchType}/{x}/{y}”、new{controller=“Home”、action=“Search”}、//默认值new{page=@“\d+”}//约束:页面必须是数字);MapRoute(null,{q}/{SearchType}/Page{Page},new{controller=“Home”,action=“Search”},//默认值new{Page=@“\d+”}//约束:页面必须是数字);MapRoute(null,“{controller}/{action}”);