0

I'm having some problems posting multiple parameters to my controller using AJAX.I want to pass model list and button name (string) to my controller.

jQuery:

function PostForm(buttonname) {

    $.ajax({
        url: "/ControllerName/ViewName",
        type: "POST",
        dataType: "application/JSON",
        data: 
        JSON.stringify({ 
           listOfObjects = $('#form').serialize(),
           button : buttonname
        })
    });
};

partial view:

<input name="buttonname" value="Name" onClick="PostForm('Name')" />

Controller:

[HttpPost]
public ActionResult ViewName(List<MyObject> listOfObjects ,string button)
{
//Obj should now contain the list of objects and button name
}

On click of button,i am getting the value of button name but count of listobjects is 0.

How do I pass multiple params with different datatypes to the MVC method?

Ideas and suggestions greatly appreciated ! Thanks!

1
  • can you show your #form, also MyObject, check on fiddler which query do you send and post here? Commented Jun 18, 2013 at 7:43

4 Answers 4

4

I got the solution.

function PostForm(buttonname) {

var data = $('#form').serialize();
var finaldata = data + "&buttonclicked="+buttonname;

$.ajax({
    url: "/ControllerName/ViewName",
    type: "POST",
    data: finaldata ,
    success: success function(){},
    error : error function(){}
    });
};

partial view:

<input name="buttonname" value="Name" onClick="PostForm('Name')" />

Controller:

[HttpPost]

public ActionResult ViewName(List<MyObject> listOfObjects ,string buttonclicked)
{
//Obj should now contain the list of objects and button name
}

the problem was i was using same name for button's name and input paramtere of the POST method in controller.Thats why i was getting list of all the button's names. Neways its working now..

thanks for the help!!!

Sign up to request clarification or add additional context in comments.

Comments

0

function PostForm(buttonname) {

$.ajax({
    url: "/ControllerName/ViewName",
    type: "POST",
    dataType: "application/JSON",
    data: JSON.stringify($('#form').serialize())
});

};

[HttpPost]
public ActionResult ViewName(FormCollection formCollection)
{
     // use formCollection["yourcontrol"] to get your post value
}

1 Comment

i am having a grid which contains buttons.so i wont be able to find the post value of it using formcollection[].
0

Try this:

View:

function PostForm(buttonname) {

    $.ajax({
        url: "/ControllerName/ViewName",
        type: "POST",
        dataType: "application/JSON",
        data: { listOfObjects: data: $('#Form').serilize(), button: buttonname },
        JSON.stringify({ 
           listOfObjects = $('#form').serialize(),
           button : buttonname
        })
    });
};

Controller:

[HttpPost]
public ActionResult ViewName(MyObject[] listOfObjects ,string button)
{

}

Comments

0

form.serialize() works alone. With additional data, It does not work. remove dataType and try like this:

data: $('#Form').serilize() + "&button=" + buttonname

hope it helps.

1 Comment

sorry... i was using the same name as button's name.Getting the values now.

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.