0

I have some values on the query string of the form page. When I submit the form using the submit button it self I can get the query string values but when I submit the form using jquery as below. I don't see the query string values anymore. Am I messing something?

      $('#PasswordResetForm').submit(function () {

         if (some conditions) {
            $('#PasswordResetForm').submit();
         }
         else {             
            return false;
         }
      });

the link is something like this:

 http://websitename/Account/PasswordReset/?username=username&token=ZnsHnVh3oIOwOR2GUaGfsA2

html code is as below:

@using (Html.BeginForm("PasswordReset","Account",FormMethod.Post,new        
                                                      {id="PasswordResetForm"}))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary("", new { id = "validation" })

    <fieldset>
        <legend ">Enter your User Name</legend>

        <br />

        <table id="MainTable" border="0">
            <tr class="MainTr">
                <td class="FirstTd">
                    @Html.Label("User Name")
                </td>
                <td class="SecondTd">

                </td>

            </tr>
            <tr class="MainTr">
                <td class="FirstTd">
                    New Password
                </td>
                <td class="SecondTd">
                    @Html.Password("newPassword")
                </td>

            </tr>
            <tr class="MainTr">
                <td class="FirstTd">
                    Confirm New Password
                </td>
                <td class="SecondTd">
                    @Html.Password("confirmNewPassword")
                </td>

            </tr>                
            <tr class="MainTr">                                        
                <td colspan="2">
                    <input type="submit" id="submitBt" value="Recover Password" />
                </td>                    
            </tr>
        </table>
    </fieldset>
}

I can't see the token on the server anymore. when I use jquery to submit the form. I'm using mvc 4 and jquery 2.0.0

6
  • Can you add submit button code Commented Aug 5, 2013 at 2:01
  • @AKA I've added the mvc side code as well Commented Aug 5, 2013 at 2:12
  • @AKA I've added the html code Commented Aug 5, 2013 at 2:32
  • what "query strings" are you expecting on the server that you are not receiving? token value is the one you are expecting right? Is this view called "PasswordReset"? Commented Aug 5, 2013 at 2:34
  • yes... I'm expecting to get token from query string. but request.querystring.count is 0. Commented Aug 5, 2013 at 2:38

6 Answers 6

1

Try This

$(function()
{

    function  ValidateData() {
             if (some conditions) {
                return true;
             }
             else {             
                return false;
             }
          }
});

HTML Changes

 <input type="submit" id="submitBt" value="Recover Password" onClick="return ValidateData();" />
Sign up to request clarification or add additional context in comments.

2 Comments

I'm going to test this and I'll back to you.
it's working now... I really don't understand why the query string are removed when we call a specific action.
1

Use the following code:

$('#PasswordResetForm').submit(function () {
    if (some conditions) {
        this.submit();
    }
    else {             
        return false;
    }
});

Calling $('#PasswordResetForm').submit(); calls the same function again. Don't wrap it as a jquery object.

Preserve the query strings setting the query strings as hidden elements:

@Html.Hidden("username", Request.QueryString["username"])
@Html.Hidden("token", Request.QueryString["token"])

8 Comments

I'm going to test this and I'll back to you.
Sorry... no use. I still can't see the querystring parameters.
If i remove the form parameter how can I call the form in jquery?
Just add the query strings as hidden elements
there should be a way to get the querystring parameters in the server side without extra work in the client side, but if I couldn't find any other way I think I have to add a hidden field as you said.
|
1

Querystring parameters are available using JavaScript's location object's search property, i.e. location.search. From there you can work with that in your submit function.

e.g. http://somesite.com/?querystringParam1=someVal&querystringParam2=someOtherVal

location.search will equal ?querystringParam1=someVal&querystringParam2=someOtherVal

If you need the querystring in a .NET like NameValueCollection, I wrote a quick JSFiddle, http://jsfiddle.net/nickyt/NnyV2/5/

(function() {
    function getQuerystringAsNameValueCollection(querystring) {
       var querystringKeyValuePairs = querystring.split('&');
       var querystringHashTable = {};

       for (var index = 0; index < querystringKeyValuePairs.length; index++) {
           var keyValue = querystringKeyValuePairs[index].split('=');
           querystringHashTable[keyValue[0]] = keyValue[1];
       }
    }

    $('#PasswordResetForm').submit(function (e) {
             var querystring = getQuerystringAsNameValueCollection(location.search.substring(1));

             if ("token" in querystring) { // Do more checks on the token if necessary
                $('#PasswordResetForm').submit();
             }
             else {             
                e.preventDefault();
             }
          });
})();

2 Comments

Thanks nichyt for your answer, but I already found my answer from AKA. sorry
@Daniel, I know it was already answered, but it's better to bind events for separation of concerns, but as well ,multiple binds can be done on an element. And this can all be done without modifying markup. You could do all this in an external JS file. Some more here stackoverflow.com/questions/6941483/onclick-vs-event-handler or just Google "unobtrusive javascript"
0
$('#PasswordResetForm').submit(function (e) {
    e.preventDefault();
    if (some conditions) {
        this.submit();
    }
    else {             
        return false;
    }
});

2 Comments

I'm going to test this and I'll back to you.
Sorry... no use. I still can't see the querystring parameters.
0

Use also the .on click event listener of jquery like

$('#PasswordResetForm').on('click', function() { 
   //code here.....
});

Additional knowledge. :)

Comments

-1

This makes no sense:

$('#PasswordResetForm').submit(function () {
   $('#PasswordResetForm').submit();
}

Because you are raising the event again.

You just need to return true, i.e.:

$('#PasswordResetForm').submit(function () {
   if (cond)
      return true;
   return false;
}

http://api.jquery.com/submit/

1 Comment

the form is being submitted and I can see all the FormCollections in the server side. All I need is to be able to get the QueryStrings as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.