views:

93

answers:

3

I have a page that does a JSON result, get and post method in the controller with two submit buttons. One button goes to the Post method only for a redirect, and the other button goes to the JsonResult method(named AddTableData). How do I set this up in my JQuery code?

$('#firstSubmit').click(function() {
    $(document).submit(function() {
    });
});

$('#secondSubmit').click(function() {
    $('#addTable').submit(function() {
        $.post($(this).attr("action"), $(this).serialize(), function(response) {
            ...load Table
        });
        return false;
    });
});

<%using (Html.BeginForm("LoadTable", "Home", FormMethod.Post, new { id = "addTable" })) %>
    <td><input id="Submit1" type="submit" name="firstSearch" value="Search" /></td>
    <td><input id="Submit2" type="submit" name="secondSearch" value="Search" /></td>

How do I use the firstSubmit to hit the post only, and the second submit to work the JsonResult only?

EDIT

$('#secondSubmit').click(function() {
    $.getJSON("Home/AddTableData", {Name:"Name on Document"}, function(json) {
        alert("good");
    });
});    

My variables that usually get picked up on a get/post function is gone. How do I post them to my JSon function?

+2  A: 

Assuming I am understanding you correctly I have 2 answers for you...

  1. Why use JavaScript just to post a form without Ajax?

  2. If you must post the form using JavaScript, submit the form, not the document, so something like:

    $('form').submit();

Gregg
The form is being posted to the JSonResult. "addTable. The post is on the Index method controller.
You are correct. I made that correction and is now using one as a submit type and the other as a button. That takes care of the problem. Thanks!!
+1  A: 
$('#firstSubmit').click(function() {
  $('#addTable').submit();
});

$('#secondSubmit').click(function() {
  $.getJSON("Home/AddTableData", $('#addTable').serialize(), function(json) {
    alert("good");
  });
});


<%using (Html.BeginForm("LoadTable", "Home", FormMethod.Post, new { id = "addTable" })) %>
<td><input id="Submit1" type="button" name="firstSearch" value="Search" /></td>
<td><input id="Submit2" type="button" name="secondSearch" value="Search" /></td>
ovais.tariq
I never thought about that. I am not going to my JsonResult method however (AddTableData). Is there away to redirect there?
i m sorry didnt get you,, could u explain further,
ovais.tariq
I think I am getting closer. I am getting to the JsonResult method, but the variables I usually pass from my form is now null. Do you know how to do that? See my New Edits
i have edited the function, does it help u now?
ovais.tariq
A: 

I have not tested this:

$('#secondSubmit').click(function() {
    $url = 'Home/AddTableData/?'+$('#add_table').children('input').serialize()
    $.getJSON($url, {Name:"Name on Document"}, function(json) {
        alert("good");
    });
});    
Mathletics