0

I need to send to my Controller string with json data, but parameters of this string may be changed so I must create not Json primitives but string with json data that look like that:

var from = $("select#optionsfrom").val();
            var destfrom = "'" + $("select#optionfrom option:selected").attr("name") + "':";
            var to = $("select#optionsto").val();
            var destto = "'" + $("select#optionsto option:selected").attr("name") + "':";
            var json = "{" + destfrom + from + ", " + destto + to + ", 'DepartureDate':" + $("#departure").val() + ", 'ReturnDate':" + $("#return").val() + "}";
            $.ajax({
                url: "/Home/GetFlights",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(json),
                type: "post",
                cache: false,
                success: function (result) {
                    self.prices(result);
                },
                error: function (xhr, status, error) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
                }
            });

It's clear when I'm using Json primitives, but I can't get how should I accept this string in my Controller?

Here is Controller (please note that json parameter should accept json string, but I'm not sure that it is correct way).

public async Task<JsonResult> GetFlights(string json, DateTime DepartureDate, DateTime ReturnDate)
        {
            byte[] stream = HttpServerUtility.UrlTokenDecode(Request.Cookies["psw"].Value);
            byte[] decodedValue = MachineKey.Unprotect(stream, "all");
            var psw = Encoding.UTF8.GetString(decodedValue);
            var prices = await tr.GetPrices(User.Identity.Name, psw, json, DepartureDate, ReturnDate);
            return Json(prices.PriceItems);
        }
1
  • You should probably show your controller code, particularly the method signature. You might also post your route definitions. Commented Feb 19, 2015 at 16:16

1 Answer 1

0

If you are just passing var json through to another service and not processing it yourself:

var json = "{" + destfrom + from + ", " + destto + to + ", 'DepartureDate':" + $("#departure").val() + ", 'ReturnDate':" + $("#return").val() + "}";
$.ajax({
    url: "/Home/GetFlights",
    data: { json: json },
    type: "post",
    success: function (result) {  }
});

And you don't need to change your action

public async Task<JsonResult> GetFlights(string json, DateTime DepartureDate, DateTime ReturnDate)
{
    var prices = await tr.GetPrices(User.Identity.Name, psw, json, DepartureDate, ReturnDate);
    ...
}

Also note that the single quotes are not valid JSON.

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

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.