可以在MVC中的控制器操作中设置json对象的值和键吗?

时间:2021-12-02 19:31:56

Is it possible for me to get the same SelectListItems in this list:

我可以在此列表中获取相同的SelectListItems:

    public static List<SelectListItem> GetAreaApprovingAuthorities(int id)
    {
        List<Employee> approvingAuthorities = new List<Employee>();
        using (var db = new TLMS_DBContext())
        {
            approvingAuthorities = db.Employees.Where(e => e.UserRoleID > 1 && e.PersonnelAreaID == id).ToList();
        }
        List<SelectListItem> returned = new List<SelectListItem>();
        foreach (Employee emp in approvingAuthorities)
        {
            returned.Add(new SelectListItem { Text = string.Format("{0} {1}", emp.FirstName, emp.LastName), Value = emp.ID.ToString() });
        }
        return returned;
    }

and pass them into a select list using Json? Here is the controller action where the List is acquired:

并使用Json将它们传递到选择列表中?以下是获取List的控制器操作:

    public JsonResult GetApprovingAuthorities(int id)
    {
        return Json(TLMS_DropDownLists.GetAreaApprovingAuthorities(id),JsonRequestBehavior.AllowGet);
    }

Here is where the json object is iterated through and then passed to the select list (this is triggered when another select list's value is changed):

这里是json对象迭代的地方,然后传递给选择列表(当另一个选择列表的值发生更改时触发):

            $.ajax({
            type: 'GET',
            data: { id: selectedValue },
            url: '@Url.Action("GetApprovingAuthorities")',
            contentType: "application/json; charset=utf-8",
            global: false,
            async: false,
            dataType: "json",
            success: function (jsonObj) {
                                $('#aa').empty();
                                $.each(jsonObj, function (key, value) {
                                    $('#aa').append($("<option/>", {
                                        value: key.Text,
                                        text: value.Text
                                    }));
                                });
                            }
        });

This is working to populate the "aa" select list and I am receiving the select list's selected item via the FormCollection in a controller action, but I cannot capture the original ID from the "GetAreaApprovingAuthorities" SelectListItem's Value. Is there a way that I can make this happen?

这是填充“aa”选择列表,我通过控制器操作中的FormCollection接收选择列表的选定项目,但我无法从“GetAreaApprovingAuthorities”SelectListItem的值中捕获原始ID。有没有办法可以实现这一目标?

1 个解决方案

#1


1  

When you're iterating on the jsonObj it should be like this

当您在jsonObj上进行迭代时,它应该是这样的

//the first parameter is just the index of the iteration
//and the second one is the json object (SelectListItem)
$.each(jsonObj, function (index, obj) { 
    $('#aa').append($("<option/>", 
    {
          value: obj.Value,
          text: obj.Text
    }));
});

#1


1  

When you're iterating on the jsonObj it should be like this

当您在jsonObj上进行迭代时,它应该是这样的

//the first parameter is just the index of the iteration
//and the second one is the json object (SelectListItem)
$.each(jsonObj, function (index, obj) { 
    $('#aa').append($("<option/>", 
    {
          value: obj.Value,
          text: obj.Text
    }));
});