Geerthan Logarajah
Geerthan Logarajah

Reputation: 34

Pass Json Object and String Value at once in Ajax

I want to pass a Json Object and String Value together in ajax call. I have attached my code below.

$('#ddcountryid').change(function () {
            var jdata = ko.mapping.toJSON(viewModel);
            var Cid = $(this).val();
            //alert(intCountry);
            $.ajax({
                url: '@Url.Action("PopulateDropDown", "Pepmytrip")',
                type: 'POST',
                data: jdata,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.redirect) {
                        location.href = resolveUrl(data.url);

                    }
                    else {
                        ko.mapping.fromJS(data, viewModel);
                    }
                },
                error: function (error) {
                    alert("There was an error posting the data to the server: " + error.responseText);
                },

            });
        });

My Action Method

 public JsonResult PopulateDropDown(Wrapper wrapper, string cID)
        {
            wrapper.Destinations = new SelectList(context.Destinations.Where(c=>c.CountryId == cID), "id", "Name").ToList();
            return Json(wrapper);
        }

I am getting wrapper object with Values, but how can get the CID as well. Can anyone please help me on this??.

Upvotes: 1

Views: 1433

Answers (3)

Marian Ban
Marian Ban

Reputation: 8168

you can pass it as query string parameter:

var Cid = $(this).val();
$.ajax({
  url: '@Url.Action("PopulateDropDown", "Pepmytrip")' + '?cID=' + Cid, 
  ...

server side:

public JsonResult PopulateDropDown(Wrapper wrapper, string cID)
        {
            wrapper.Destinations = new SelectList(context.Destinations.Where(c=>c.CountryId == cID), "id", "Name").ToList();
            return Json(wrapper);
        }

OR add a new property to your Wrapper object as Gavin Fang suggested:

var Cid = $(this).val();
viewModel.Cid = Cid;
var jdata = ko.mapping.toJSON(viewModel);

server side code:

public JsonResult PopulateDropDown(Wrapper wrapper)
        {
            var cid = wrapper.Cid;
            wrapper.Destinations = new SelectList(context.Destinations.Where(c=>c.CountryId == cID), "id", "Name").ToList();
            return Json(wrapper);
    }  

Upvotes: 1

Shastry
Shastry

Reputation: 446

You can achieve this is two ways:

  1. You can add extra field for existing json 'jdata' by defining field jdata.cid = null; and assigning it as jdata.cid = $(this).val();.

  2. Prepare an object to hold both json data and string value: var obj = {"json": jdata, "string":$(this).value()}; then pass obj in data parameter

Upvotes: 0

Gavin Fang
Gavin Fang

Reputation: 367

I think you can add a property to store you 'CID' to your viewModel.

and you can get the CID in the 'success' function in javascript in here, I think.

Upvotes: 0

Related Questions