简体   繁体   English

jQuery插件,JavaScript语法,级联下拉MVC4跟踪,多级层叠

[英]JQuery plug in, JavaScript syntax, cascading dropdown MVC4 followup, multi-level cascade

First of all, this post Cascading drop-downs in MVC 3 Razor view has been very helpful. 首先,此文章在MVC 3 Razor视图中的层叠下拉菜单非常有用。 I have used the initial syntax, and have gotten my cascading drop down to work. 我已经使用了初始语法,并且得到了级联下拉菜单以进行工作。 I have added more parameters to the functions that are called in my controller, to change the list contents. 我向控制器中调用的函数添加了更多参数,以更改列表内容。 Here is a View that does NOT use the "plugin". 这是一个不使用“插件”的视图。

@model HolterManagementUI.Models.CrudUserViewModel
@{
ViewBag.Title = "Edit";
}
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
$(function () {
    $('#SelectedDivisionID').change(function () {
        var selectedDivisionID = $(this).val();
        $.getJSON('@Url.Action("Regions")', { divisionId: selectedDivisionID, isActive: true }, function (regions) {
            var regionsSelect = $('#SelectedRegionID');
            regionsSelect.empty();
            $.each(regions, function (index, region) {
                regionsSelect.append(
                    $('<option/>')
                        .attr('value', region.ID)
                        .text(region.Description)
                );
            });
         var locationsSelect = $('#SelectedLocationID');
         locationsSelect.empty();
        });
    });

    $('#SelectedRegionID').change(function () {
        var selectedRegionID = $(this).val();
        $.getJSON('@Url.Action("Locations")', { regionId: selectedRegionID, isActive: true }, function (locations) {
            var locationsSelect = $('#SelectedLocationID');
            locationsSelect.empty();
            $.each(locations, function (index, location) {
                locationsSelect.append(
                    $('<option/>')
                        .attr('value', location.ID)
                        .text(location.Description)
                );
            });
        });
    });
});
</script>
<h2>Edit User</h2>
@using (Html.BeginForm())
{

<table>
<tr>
    <td>LanID</td>
    <td>
    @Html.HiddenFor(h => h.ID)
    @Html.EditorFor(h => h.LanID)</td>
</tr>
<tr>
    <td>Division</td>
    <td>
    @Html.DropDownListFor(h => h.SelectedDivisionID, new SelectList(Model.Divisions, "ID", "Description", Model.SelectedDivisionID.ToString()))
    </td>
</tr>
<tr>
    <td>Region</td>
    <td>
    @Html.DropDownListFor(h => h.SelectedRegionID,  new SelectList(Model.Regions, "ID", "Description", Model.SelectedRegionID.ToString()))
    </td>
</tr>
<tr>
    <td>Location</td>
    <td>
    @Html.DropDownListFor(h => h.SelectedLocationID,  new SelectList(Model.Locations, "ID", "Description", Model.SelectedLocationID.ToString()))
    </td>
</tr>
<tr>
    <td>Is Active</td>
    <td>@Html.EditorFor(h => h.IsActive)</td>
</tr>
</table>

<div class="buttongroup" align="left" style="margin-top: 50px">
    <input type="submit" name="submitButton" value="Save" />
    <button type="button" onclick="location.href='@Url.Action("List")'">Cancel</button>
</div>
}

What I am unsure how to do is how to add the JavaScript plugin in the re-factored part, and then how to modify it so I can pass more than one parameter. 我不确定该怎么做是如何在重构部分中添加JavaScript插件,然后如何对其进行修改,以便可以传递多个参数。 I have attempted to add the code into a separate JavaScript file in my project and include it, but then the "wiring" always breaks. 我试图将代码添加到项目中的单独JavaScript文件中并包含它,但随后“接线”总是中断。

Here is my separate JavaScript file: 这是我单独的JavaScript文件:

/*!
* DropDowns.js
* Script to manage Cascading of dropdowns
* 
* From stackoverflow.com
* https://stackoverflow.com/questions/4458970/cascading-drop-downs-in-mvc-3-razor-view
*/
(function ($) {
$.fn.cascade = function (options) {
    var defaults = {};
    var opts = $.extend(defaults, options);

    return this.each(function () {
        $(this).change(function () {
            var selectedValue = $(this).val();
            var params = {};
            params[opts.paramName] = selectedValue;
            $.getJSON(opts.url, params, function (items) {
                opts.childSelect.empty();
                $.each(items, function (index, item) {
                    opts.childSelect.append(
                        $('<option/>')
                            .attr('value', item.Id)
                            .text(item.Name)
                       );
                    });
                });
            });
        });
    };
})(jQuery);

Here is my view that is attempting to use the DropDowns.js file: 这是我尝试使用DropDowns.js文件的视图:

@model HolterManagementUI.Models.CrudUserViewModel
@{
ViewBag.Title = "Create";
}

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/scripts/DropDowns.js")

<script type="text/javascript">
$(function () {
    $('#SelectedDivisionID').cascade({
        url: '@Url.Action("Regions")',
        paramName: 'divisionId',
        childSelect: $('#SelectedRegionID')
    });

    $('#SelectedRegionID').cascade({
        url: '@Url.Action("Locations")',
        paramName: 'regionId',
        childSelect: $('#SelectedLocationID')
    });
});
</script>
<h2>Create a User</h2>
@using (Html.BeginForm())
{

<table>
<tr>
    <td>LanID</td>
    <td>
    @Html.HiddenFor(h => h.ID)
    @Html.EditorFor(h => h.LanID)</td>
</tr>
<tr>
    <td>Division</td>
    <td>
    @Html.DropDownListFor(h => h.SelectedDivisionID, new SelectList(Model.Divisions, "ID", "Description"))
    </td>
</tr>
<tr>
    <td>Region</td>
    <td>
    @Html.DropDownListFor(h => h.SelectedRegionID,  Enumerable.Empty<SelectListItem>())
    </td>
</tr>
<tr>
    <td>Location</td>
    <td>
    @Html.DropDownListFor(h => h.SelectedLocationID, Enumerable.Empty<SelectListItem>())
    </td>
</tr>
</table>

<div class="buttongroup" align="left" style="margin-top: 50px">
    <input type="submit" name="submitButton" value="Save" />
    <button type="button" onclick="location.href='@Url.Action("List")'">Cancel</button>
</div>
}

So, 1. How do I get the plugin code to work? 因此,1.如何使插件代码正常工作? 2. How can I add more parameters to the plugin method for cascade? 2.如何为插件方法添加更多参数以进行级联? 3. Is this worth doing? 3.这值得吗?

First, for your broken plugin, I think you are trying to render to script for the dropdown.js incorrectly as you are treating is like a bundle when I believe it is not part of your bundle based on your syntax: 首先,对于您损坏的插件,我认为您尝试错误地渲染dropdown.js脚本,因为根据语法,我认为它不属于捆绑软件,因此您将其视为捆绑软件:

Change: 更改:

@Scripts.Render("~/scripts/DropDowns.js")

To: 至:

<script type="text/javascript" src="@Url.Content("~/Scripts/DropDown.js")"></script>

Second, it appears that your second parameter is hardcoded, why not pass it as part of the URL you are generating for the plugin to use? 其次,似乎您的第二个参数是硬编码的,为什么不将其作为生成的URL的一部分传递给插件使用呢?

$('#SelectedDivisionID').cascade({
        url: '@Url.Action("Regions", "Controller", new{isActive = true})',
        paramName: 'divisionId',
        childSelect: $('#SelectedRegionID')
    });

Third, if you are using this code in multiple places, then yes, absolutely it would be worth not having to write what you did your first code block over and over again. 第三,如果您在多个地方使用此代码,那么是的,绝对不必一次又一次地编写第一个代码块的内容。 This reduces complexity, points of failure and should speed up deployment of cascading dropdowns going forward in your project. 这样可以降低复杂性和故障点,并可以加快项目中级联下拉菜单的部署。

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

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