简体   繁体   English

MVC4上的级联下拉列表不更新也不加载JavaScript文件

[英]cascading dropdown list on mvc4 not updating nor loading javascript file

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 我目前正在按照此教程尝试创建一组新的级联下拉列表: 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. 问题是它不起作用,我基本上对javascript不了解(目前我没有时间坐下来学习它),因此下拉列表不起作用,只有第一个显示第一个层次结构信息。

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. 对于我已经从javascript函数中了解到的内容,具有页面标签和子菜单项的div应该在页面加载后显示为隐藏,并在用户从第一个列表中选择父项后显示。而是在页面加载后所有内容都显示出来。 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? 同样,一旦我选择了一个父对象,第二个列表就什么也没有发生, 我可以由此推断出javascrip文件没有在用户浏览器中执行,为什么它没有被执行? what am I doing wrong? 我究竟做错了什么?

Thank in advance, any help will be appreciated. 在此先感谢您的帮助。

You have an error on the following line 您在以下行有一个错误

items += "<option value='" + child.value + "'>" + child.Name + "</option>";

Which should be: 应该是:

items += "<option value='" + child.Value + "'>" + child.Text + "</option>";

Since your controller action is returning a SelectList , this class is an IEnumerable<SelectListItem> where SelectListItem has 2 properties called Value and Text . 由于您的控制器操作正在返回SelectList ,因此此类是IEnumerable<SelectListItem> ,其中SelectListItem具有2个名为ValueText属性。

There's also another issue with your code. 您的代码还有另一个问题。 You have used the folllowing to construct the url to your controller action: 您已使用以下方法构造了控制器操作的网址:

URL + '/' + $('#ParentsID').val()

which will result in an url of the form: 这将导致以下形式的网址:

/SomeController/Children/123

But the action argument of your Children action is called parentId . 但是, Children操作的操作参数称为parentId If you are using the default route setup, only an {id} parameter will be sent as part og of the uri path (that's the default route pattern: {controller}/{action}/{id} and not {controller}/{action}/{parentid} ). 如果您使用的是默认路由设置,则只会在uri路径的一部分中发送一个{id}参数(这是默认路由模式: {controller}/{action}/{id}而不是{controller}/{action}/{parentid} )。 So you should either change your route setup or pass the parentId parameter like this: 因此,您应该更改路由设置或像这样传递parentId参数:

$.getJSON(URL, { parentId: $('#ParentsID').val() }, function (data) {
    ...
});

Yet another issue with your code is that your Children conrtoller action is decorated with the [HttpPost] attribute but the $.getJSON method sends a GET request. 您的代码的另一个问题是,您的Children conrtoller操作使用[HttpPost]属性修饰,但是$.getJSON方法发送GET请求。 So it won't work. 因此,它将无法正常工作。 You could use the $.post method instead: 您可以改用$.post方法:

$.post(URL, { parentId: $('#ParentsID').val() }, function (data) {
    ...
});

Another issue is that you are showing the StatesDivID but your actual div that was hidden is ChildrenDivId . 另一个问题是您显示的是StatesDivID但是隐藏的实际div是ChildrenDivId Make sure you are showing the proper element: 确保显示正确的元素:

$('#ChildrenDivId').show();

Also make sure you have the jQuery script referenced before your parentChildren.js script. 还要确保在parentChildren.js脚本之前引用了jQuery脚本。 Right now you have included your script inside the view but cannot see jQuery being included. 现在,您已将脚本包含在视图中,但看不到包含jQuery。 Maybe you have it in your _Layout. 也许您将它放在_Layout中。

I would recommend you using a javascript debugging tool such as FireBug or Chrome developer toolbar to analyze your javascript code. 我建议您使用诸如FireBug或Chrome开发者工具栏之类的JavaScript调试工具来分析您的JavaScript代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM