ASP.NET MVC - restarting FormMethod.Get querystring?

I have a simple form with one text box and one submit button. The form basically sends to another page with the value in the text box as a request. When I click the submit button, the request is executed 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 mine HomeControllerthat accepts "TargetCode" as the query string, and I already configured routing in Global.ascx.csfor this. What needs to be done to rewrite the request so that it does not display this URL? TargetCode = in URL? Here is the code for the form:

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

Thanks, Kenny.

+3
source share
1 answer

, jQuery , , URL. , hook .

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

Source: https://habr.com/ru/post/1720212/


All Articles