【问题标题】:cascading dropdown not working级联下拉菜单不起作用
【发布时间】:2015-10-13 13:45:42
【问题描述】:

我正在尝试在 mvc 中使用级联下拉列表。一些它是如何工作的,但有一些问题。实际上,当呈现第一个视图时,我有一个基于此下拉列表的下拉帐户类型,我必须填充组下拉列表。但是当第一个下拉菜单被渲染时,它必须默认选择房地产经纪人和赞助商,房地产经纪人是第一个选择的选项,然后所有与之关联的组都应该被渲染。但无法在第一时间获得价值。有什么问题可以建议我。

这是我的代码

public class ViewModel
{
 [Required]
        public SelectList AccountType { get; set; }
        public string SelectedAccountType { get; set; }
        public IEnumerable<SelectListItem> Group { get; set; }
        public string SelectedGroup { get; set; }
}


[HttpGet]
        public ActionResult Index(ViewModel viewModel)
        {  



            List<SelectListItem> groups = sDbContext.Groups
                    .Where(o => o.GroupId > 1 && o.Deleted == false && o.AccountSubId == 1).OrderBy(uu => uu.GroupName)
                    .Select(o => new SelectListItem()
                    {
                        Value = o.GroupId.ToString(),
                        Text = o.GroupName
                    }).ToList();
            groups.Insert(0, new SelectListItem() { Value = "0", Text = "All" });
            viewModel.Group = groups;
            viewModel.AccountType = new SelectList(sDbContext.AccountTypes.Where(o => o.AccountTypeId != 0), "AccountTypeId", "AccountName");                          
            reportService.GetReportsListGrid();
            return View(viewModel);           
    }

查看:

@model App1.ViewModels.ViewModel
@{
    List<SelectListItem> list = new List<SelectListItem>();   
}
@using (Html.BeginForm("Index", "routee", FormMethod.Post, new { @id = "appForm" }))
{
 Account Type
                                @Html.DropDownListFor(m => m.SelectedAccountType, (IEnumerable<SelectListItem>)Model.AccountType, new { @class = "form-control" })
 @Html.DropDownListFor(model => model.SelectedGroup, list, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.SelectedGroup)

}

这是我的脚本..

<script type="text/javascript">
 $('#SelectedAccountType').change(function () {
        OnAccountTypeChange();
    });

    function OnAccountTypeChange() {
        var id = $("#SelectedAccountType :selected").val();
        var groupId = $('#SelectedGroup').val();
        if (id != "") {
            alert(id);
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: '@Url.Action("GetGroupsByAccountSubTypeId", "Common", new { area = "" })',
                data: { "AccountSubId": id },
                dataType: "json",
                beforeSend: function () {
                    //alert(id);
                },
                success: function (data) {
                    var items = "<option value='0' selected='selected'>-All-</option>";
                    $.each(data, function (i, groups) {
                        items += "<option value='" + groups.Value + "'>" + groups.Text + "</option>";
                    });
                    $('#SelectedGroup').html(items);
                    if (groupId != undefined || groupId != null || groupId != "")
                        $('#SelectedGroup').val(groupId);
                },
                error: function (result) {
                    console.log('Service call failed: ' + result.status + ' Type :' + result.statusText);
                },
                complete: function () {
                    $('#SelectedGroup').prop('disabled', false);
                }
            });
        }
        else {
            $('#SelectedGroup').html(items);
        }
    }
</script>

【问题讨论】:

标签: jquery asp.net-mvc drop-down-menu


【解决方案1】:

只是一个简单的错误。你为什么在这里使用列表

@Html.DropDownListFor(model => model.SelectedGroup, list, new { @class = "form-control" })

你的代码应该是这样的:

@Html.DropDownListFor(model => model.SelectedGroup, (IEnumerable<SelectListItem>)Model.Group, new { @class = "form-control" })

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    相关资源
    最近更新 更多