1

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??.

3 Answers 3

1

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);
    }  
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

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.