I'm currently trying to create a new set of cascading dropdown lists by following this tutorial : http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx
problem is it doesnt work and I'm basically illiterate on javascript (currently I dont have the time to sit and learn it), somehow the dropdown lists are not working, only the first one shows the first hierarchy info.
This is what I've done:
First the controller:
Here's the index method:
public ActionResult Index()
{
var list = repo.GetParentEstablishments();
ViewBag.Parent = (new SelectList(list.ToArray(),"EstablishmentId","Name"));
return View();
}
Here's the method that's supposed to return the list of children for the selected father:
[HttpPost]
public ActionResult Children(int parentId)
{
var parents = repo.GetChildrenEstablishments(parentId);
return Json(new SelectList(parents, "EstablishmentId", "Name"));
}
Here's the view for the index method:
@using (Html.BeginForm("Index", "Ticket", FormMethod.Post, new { id = "ParentChildrenFormID", data_childrenListAction = @Url.Action("ChildrenList") }))
{
<fieldset>
<legend> Parent/Children</legend>
@Html.DropDownList("Parents", ViewBag.Parent as SelectList, "Select a Parent", new {id="ParentsID"})
<div id="ChildrenDivId">
<label for="Children">Children </label>
<select id="ChildrenID" name="Children"></select>
</div>
<p>
<input type ="submit" value="Submit" id="SubmitID" />
</p>
</fieldset>
}
<script src ="@Url.Content("~/Scripts/parentChildren.js")"></script>
And finally here's the script file:
$(function () {
$('#ChildrenDivId').hide();
$('#SubmitID').hide();
$('#ParentsID').change(function () {
var URL = $('#ParentChildrenFormID').data('childrenListAction');
$.getJSON(URL + '/' + $('#ParentsID').val(), function (data) {
var items = '<option>Select a Children</option>';
$.each(data, function (i, child) {
items += "<option value='" + child.value+ "'>" + child.Name + "</option>";
// state.Value cannot contain ' character. We are OK because state.Value = cnt++;
});
$('#ChildrenID').html(items);
$('#StatesDivID').show();
});
});
$('#ChildrenID').change(function () {
$('#SubmitID').show();
});
});
For what I've managed to understand from the javascript function, the div that has the label and dropdown for the children should appear hidden once the page is loaded and appear once the user selects a parent from the first list, currently this is not happening and instead everything shows up once the page is loaded. Also once I select a parent nothing happens on the second list, I can infer from this that the javascrip file is not being executed on the users browser, why isn't it being executed? what am I doing wrong?
Thank in advance, any help will be appreciated.