Javascript jQuery组合框插件-如何从jQuery组合框获取getSelectedIds

Javascript jQuery组合框插件-如何从jQuery组合框获取getSelectedIds,javascript,html,jquery,asp.net-core,Javascript,Html,Jquery,Asp.net Core,我在一个html中使用两个Jquery组合框对象。如何将每个组合对象中的SelectedId设置为相应的隐藏输入对象。这里,从ddlDepotMapping中选择的ID应该存储在hfDepotMapping中。此外,从ddlSalaryMapping中选择的ID应存储在hfSalaryMapping中。但是当加载html页面时,在Jquery ComboBox selected属性上没有选择view bag list项 这是密码 public IActionResult UserSetup {

我在一个html中使用两个Jquery组合框对象。如何将每个组合对象中的SelectedId设置为相应的隐藏输入对象。这里,从ddlDepotMapping中选择的ID应该存储在hfDepotMapping中。此外,从ddlSalaryMapping中选择的ID应存储在hfSalaryMapping中。但是当加载html页面时,在Jquery ComboBox selected属性上没有选择view bag list项

这是密码

public IActionResult UserSetup
{
     List<GoUserDepartLink> mapping = _unitOfWork.UserDepartmentLink.GetAllDepartmentsByUserWithDepot(id);
     List<UserSalaryLocationLink> salaryMapping = _unitOfWork.User.GetUserSalaryLocations(id);
    user.SelectedDepotMapping = String.Join(",", mapping.Select(x => x.DepotNo + "." + x.DepartmentID).ToArray()) + "," + String.Join(",", mapping.Select(x => x.DepotNo).Distinct().ToArray());
user.SelectedSalaryMapping = String.Join(",", salaryMapping.Select(x => x.DepotNo + "." + x.DepartmentID).ToArray()) + "," + String.Join(",", salaryMapping.Select(x => x.DepotNo).Distinct().ToArray());
ViewBag.DepotDepartmentMapping = "[" + user.SelectedDepotMapping + "]";
ViewBag.DepotLocationMappingForSalary = "[" + user.SelectedSalaryMapping + "]";
}

public JsonResult GetDepotDepartemntsForMap()
{
        dynamic mappingList = new List<DepotMapModel>();
        mappingList = _unitOfWork.Department.GetDepotWithDepartment();
        return Json(mappingList);
 }


public dynamic GetDepotWithDepartment()
        {
            var list = goContext.goDepartmentWorkTime.
                GroupBy(d => new { d.DepotNo, d.Depot.DepotName })
            .Select(g => new
            {
                id = g.Key.DepotNo,
                title = g.Key.DepotName,
                subs = g.Select(dd => new
                {
                    id = dd.DepotNo + "." + dd.DepartmentID,
                    title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
                }).ToList()
            }).ToList();
            
            return list;
        }
public IActionResult用户设置
{
列表映射=_unitOfWork.UserDepartmentLink.GetAllDepartmentsByUserWithDepot(id);
List salaryMapping=\u unitOfWork.User.GetUserSalaryLocations(id);
user.SelectedDepotMapping=String.Join(“,”,mapping.Select(x=>x.DepotNo+”+x.DepartmentID.ToArray())+“,+String.Join(“,”,mapping.Select(x=>x.DepotNo.Distinct().ToArray());
user.SelectedSalaryMapping=String.Join(“,”,salaryMapping.Select(x=>x.DepotNo+”+x.DepartmentID.ToArray())+“,+String.Join(“,”,salaryMapping.Select(x=>x.DepotNo.Distinct().ToArray());
ViewBag.DepotDepartmentMapping=“[”+user.SelectedDepotMapping+“]”;
ViewBag.DepotLocationMappingForSalary=“[”+用户.SelectedSalaryMapping+”];
}
public JsonResult GetDepotDepartemntsForMap()
{
动态映射列表=新列表();
mappingList=_unitOfWork.Department.GetDepotWithDepartment();
返回Json(mappingList);
}
公共动态GetDepotWithDepartment()
{
var list=goContext.goDepartmentWorkTime。
GroupBy(d=>new{d.deptno,d.depton.deptname})
.选择(g=>new
{
id=g.Key.DepotNo,
title=g.Key.deptname,
subs=g.Select(dd=>new
{
id=dd.DepotNo+“+dd.DepartmentID,
title=dd.Depot.DepotNo+“+dd.Department.DepartmentName
})托利斯先生()
}).ToList();
退货清单;
}

FillDepartmentsMappings(@ViewBag.DepotDepartMapping);
FillSalaryDepotsMappings(@ViewBag.DepotLocationMappingForSalary);
//hfDepotMapping.Val(ddlDepotMapping.selectedIds)
//hfSalaryMapping.Val(ddlSalaryMapping.Selectedids)
函数FillDepartmentsMappings(默认选中){
$.ajax({
键入:“获取”,
url:“/User/GetDepotDepartemntsForMap”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
$('#ddlDepotMapping').comboTree({
资料来源:数据,
是的,
选择:对,
崩溃:是的,
选中:默认选中
});
},
故障:功能(响应){
console.log(response.responseText);
},
错误:函数(响应){
console.log(response.responseText);
}
});
}
函数FillSalaryDepotsMappings(默认选中){
$.ajax({
键入:“获取”,
url:“/User/GetDepotDepartemntsForMap”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
$('#ddlSalaryMapping').comboTree({
资料来源:数据,
是的,
选择:对,
崩溃:是的,
选中:默认选中
});
},
故障:功能(响应){
console.log(response.responseText);
},
错误:函数(响应){
console.log(response.responseText);
}
});
}

确保所选配置为阵列类型

您可以在js中设置数组,如下所示:

selected: [defaultSelected]
后端代码:

ViewBag.DepotDepartmentMapping = 1;
ViewBag.DepotLocationMappingForSalary = 10;
整个工作演示:

视图(Index.cshtml):


你好@Alan Pauil,我的回答有助于你解决问题吗?如果有,你能接受我的回答吗?如果没有,你能跟进让我知道吗?参考:。谢谢。Rena,非常感谢你的帮助。但是我的请求不同,很抱歉,如果我的问题不足以理解我在寻找什么,我如何将Javascript组合框中选择的ID设置为输入类型hfSalaryMapping,如hfSalaryMapping.Val(Javascript组合框中选择的ID)。你好@Alan Pauil,好的,我现在知道你的要求了。只需使用
$(“#hfDepotMapping”).val(默认选中)在成功函数中。请检查下面我的更新答案,但在从defaultSelected更改为[defaultSelected]后,该值不会从函数public JsonResult GetDepotDepartemntsForMap()Hi@AlanPail的返回Json(映射列表)传递,defaultSelected值来自您提供的代码中的ViewBag。它与GetDepotDepartemntsForMap返回数据无关。你在说什么?请分享正确的代码,否则我们将如何帮助您?Rena,很抱歉听到我的代码不足以给出清晰的想法。在这里,我使用两个函数GetDepotDepartemntsForMap(),GetDepotWithDepartment()来获取映射到用户的部门列表,并在显示页面时填充组合框。您已经在代码中提供了带有字符串return的字符串GetDepotDepartemntsForMap()。但是我必须使用JsonResult GetDepotDepartemntsForMap()。否则,我必须将代码改为Jsonresult,再改为string。我不知道如何将JsonResult func更改为字符串func。我现在在现有代码中添加了两个func。非常感谢好心的帮助。嗨@AlanPauil,你可能会误解一些事情。
getDepotDepartmentsFormap
方法用于获取json结果,无需关心我如何获得结果,只需确保
@model Test
<div class="controls col-sm-9">
    <input type="text" id="ddlDepotMapping" placeholder="Select" />
    <input type="hidden" id="hfDepotMapping" asp-for="SelectedDepotMapping" />
</div>

<div class="controls col-sm-9">
    <input type="text" id="ddlSalaryMapping" placeholder="Select" />
    <input type="hidden" id="hfSalaryMapping" asp-for="SelectedSalaryMapping" />
</div>

@section Scripts
{
    <link href="https://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/flatly/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.materialdesignicons.com/5.0.45/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="/lib/Drop-Down-Combo-Tree/style.css">
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <script src="~/lib/Drop-Down-Combo-Tree/comboTreePlugin.js"></script>
    <script>
        FillDepartmentsMappings(@ViewBag.DepotDepartmentMapping);
        FillSalaryDepotsMappings(@ViewBag.DepotLocationMappingForSalary);

    function FillDepartmentsMappings(defaultSelected) {
    $.ajax({
        type: "GET",
        url: "/User/GetDepotDepartemntsForMap",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $('#ddlDepotMapping').comboTree({
                source: data,
                isMultiple: true,
                cascadeSelect: true,
                collapse: true,
                selected: [defaultSelected]    //change here...   
            });
            $("#hfDepotMapping").val(defaultSelected);      //add this...
        }
        //...
    });
    }

    function FillSalaryDepotsMappings(defaultSelected) {
        $.ajax({
            type: "GET",
            url: "/User/GetDepotDepartemntsForMap",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $('#ddlSalaryMapping').comboTree({
                    source: data,
                    isMultiple: true,
                    cascadeSelect: true,
                    collapse: true,
                    selected: [defaultSelected]      //change here...
                });
                $("#hfSalaryMapping").val(defaultSelected);  //add this...
            }
            //......
        });
    }
    </script>
}
[HttpGet]
public async Task<IActionResult> Index()
{
    ViewBag.DepotDepartmentMapping = 1;
    ViewBag.DepotLocationMappingForSalary =10;   

    return View();
}

public string GetDepotDepartemntsForMap()
{
    var SampleJSONData = System.IO.File.ReadAllText("test.json");
    return SampleJSONData;
}
[
    {
        "id": 1,
        "title": "Four Wheels",
        "subs": [
            {
                "id": 10,
                "title": "Car"
            }, {
                "id": 11,
                "title": "Truck"
            }, {
                "id": 12,
                "title": "Transporter"
            }, {
                "id": 13,
        "title": "Dozer"
            }
        ]
    }, {
        "id": 2,
        "title": "Two Wheels",
        "subs": [
            {
                "id": 20,
                "title": "Cycle"
            }, {
                "id": 21,
                "title": "Motorbike"
            }, {
                "id": 22,
                "title": "Scooter"
            }
        ]
    }, {
        "id": 3,
        "title": "Van"
    }, {
        "id": 4,
        "title": "Bus"
    }
]