views:

204

answers:

1

Hello all, 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" }))

Thanks, Kenny.

A: 

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;
});
tvanfosson
Hi tvanfosson, I'm also using jQuery validate library to make sure that the textbox has value in it (required field.) When I use the jQuery function that you posted, it by-passed the validation. Here is what I have:$(document).ready(function() { $("#CodeForm").validate(); $('#CodeForm').submit(function() { window.location.href = '/' + $('[name=TargetCode]').val(); return false; });});Thanks,Kenny.
Xuan Vu
So check if the form is valid before submitting.
tvanfosson
Thanks a lot, that updated code work nicely!
Xuan Vu