He wants to pass ID of the selected item from the DDL (items are retrieved from the database in the controller to ViewBag) from the list to the controller where it will handle itself.
The first code works well if the value is a string, but for int (which have ID) is not working.
View:
@using (Html.BeginForm("DDL", "Store", FormMethod.Post,
new { id = "Form1ID",
data_PeoplAction = @Url.Action("People")
}))
{
<fieldset>
<label for="Cities">Select a city</label>
@Html.DropDownList("Cities", ViewBag.Cities as SelectList,
new { id = "CitiesID" })
</fieldset>
}
<script src="@Url.Content("~/Scripts/populat.js")"></script>
If CityCode is such as "LA", "NY" value is passed correctly, and the controller change (int ID) for the (string Code) data are correctly
ViewBag.Cities = new SelectList(DB.myBase, "CityCode", "CityName");
what does not work with ID
Controler:
public ActionResult DDL()
{
ViewBag.Cities = new SelectList(DB.myBase, "CityID", "CityName");
return View();
}
public ActionResult People(int ID)
{
var peop = from s in DB.myBase
where s.CityID == CityID
select s.Population;
ViewBag.Popul = peop;
return View();
}
populat.js:
$('#CitiesID').change(function () {
var URL = $('#Form1ID').data('PeoplAction');
$.getJSON(URL + '/' + $('#CitiesID').val(), function (data) {
});
});
The second gets good ID but I can not pass to the controller.
$('#OneID').change(function () {
var e = document.getElementById("OneID");
var strUser = e.options[e.selectedIndex].value;
}
I'll be grateful for the help