I'm trying to pass a couple of params to an mvc controller from jquery but the method is receiving null values even though the debugger shows the val() of both controls in the browser debugger are present.
$(function () {
$('#Counties').cascade({
data: JSON.stringify('{state: ' + $("#States").val() + ', county: ' + $("#Counties").val() + '}'),
url: '@Url.Action("GetCities")',
childSelect: $('#Cities')
});
});
I tried both methods in the answers below one with JSON.stringify and without. The values are still coming across as null
Here is the updated code. I read on another post that I may need to write a Route for the method. I added that but that didn't work either
$(function () {
$('#Counties').cascade({
data: {state: $("#States").val(), county: $("#Counties").val()},
url: '@Url.Action("GetCities")',
childSelect: $('#Cities')
});
});
[HttpGet]
public ActionResult GetCities(string state, string county)
{
var cities = _carrierRouteAssignmentRepository.GetCities(state, county);
return Json(cities, JsonRequestBehavior.AllowGet);
}
routes.MapRoute(
name: "GetCities",
url: "{controller}/{action}/{state}/{county}",
defaults: new { controller = "CarrierRouteAssignments", action = "GetCities", state = UrlParameter.Optional, county = UrlParameter.Optional }
);
running fiddler I see the request going out as
/CarrierRouteAssignments/GetCities?undefined=LOS+ANGELES
which is incorrect