ASP.NET MVC-重写FormMethod.Get查询字符串吗? [英] ASP.NET MVC - Rewriting FormMethod.Get querystring?

查看:88
本文介绍了ASP.NET MVC-重写FormMethod.Get查询字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表单,只有一个文本框和一个提交按钮。表单基本上发送到另一个页面,文本框中的值作为querystring。当我单击提交按钮时,查询字符串采用这种格式,例如:

I have a simple form with just one textbox and one submit button. The form basically sends to a different page with the value in the textbox as querystring. When I click on the submit button, the querystring is in this format, for example:

mysite.com/?TargetCode=Test1

我希望它以以下格式显示: mysite.com/Test1

I would like it to display in this format: mysite.com/Test1

我的 HomeController 中已经有一个动作,该动作将 TargetCode作为查询字符串,而我已经在 Global.ascx.cs 中设置了路由。我该怎么做才能重写查询字符串,以使URL中没有?TargetCode =?这是我拥有的表单的代码:

I already have an Action in my HomeController that take the "TargetCode" as the querystring, and I've already setup a routing in the Global.ascx.cs for that. What should I do to re-write the querystring so it doesn't have that "?TargetCode=" in the URL? Here is the code for the form I have:

<% using (Html.BeginForm("Index", "Home", FormMethod.Get, new { id = "CodeForm" }))


推荐答案

我认为您必须使用jQuery进行提交,否则您将依赖于浏览器构造URL的方式。 已更新,以将钩子插入到验证插件中。

I think you'd have to use jQuery to do the submission otherwise you're dependent on how the browser constructs the URL. Updated to include a hook into the validation plugin.

$('#CodeForm').submit( function() {
     var $form = $(this);
     if ($form.valid()) {
        window.location.href = $form.attr('action')
                                  + '/'
                                  + $('[name=TargetCode]').val();
     }
     return false;
});

这篇关于ASP.NET MVC-重写FormMethod.Get查询字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆