您好我的问题是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" />
}
(我删除了<字符,因此它显示在问题中) 生成的网址是http://localhost:4893/Home/Search?q=Brabant&SearchType=ZipCode&x=51&y=5,我希望它是主页/搜索/布拉班特/ ZipCode
编辑:
我不认为它与路线有关,javascript不起作用!我的问题是首先生成网址,而不是匹配它。
$('form').submit(function () {
var data = $('input[name="q"]', this).val();
window.location.href = this.action + '/' + encodeURIComponent(data);
return false;
});
答案 0 :(得分:1)
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>
}
答案 1 :(得分:0)
在Global.asax
中指定新路线 routes.MapRoute(
"SearchRoute",
"{controller}/{action}/{q}/{SearchType}/{x}/{y}",
new {controller = "Home",action = "Search",q="",SearchType=""},
);
编辑:
在您发布的路线之后,您的路线看起来很好,尝试在默认路线上方定义它们
答案 2 :(得分:0)
为什么不提交表格
@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();
});