Saravanan
Saravanan

Reputation: 7854

sending data via jQuery to ASP.Net MVC 2 Controller in mixed format

I would like to have an AJAX call with the following type,

$.ajax({
    url:"../..",
    data:{
    stringvar:$("..").val(),
    jsonobj:JSON.stringify({
    }),
    anotherstringvar:$("..").val()
    },
    type:"POST",
    content-type:"application/json"
    success:function(data){
    // do something with the data
    }
 });

How do I achieve this kind of requirement. As you find above, i have to pass normal string values along with the JSON data and i have to bind the same using ASP.NET MVC2 Model binder and JSONValueProviderFactory, which have in place.

Upvotes: 1

Views: 226

Answers (1)

Rob Angelier
Rob Angelier

Reputation: 2333

You can create a simple JSON object and send it to the server:

var data = JSON.stringify(valuetobestringified);

var json = {
    "json": data,
    "anotherstringvar": $("..").val(),
    "anotherstringvar1": $("..").val()
}

$.post("../..",json,function(){
//response from the server.
});

You can find more information about jQuery here: http://api.jquery.com/jQuery.post/

And you can create a simple JSON object with this online editor: http://jsonlint.com/

Upvotes: 1

Related Questions