ASP.NET MVC - Rewriting FormMethod.Get querystring?

大兔子大兔子 提交于 2019-12-07 16:53:35

问题


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

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

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" }))

回答1:


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;
});


来源:https://stackoverflow.com/questions/1574181/asp-net-mvc-rewriting-formmethod-get-querystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!