Asp.net mvc 无法从mvc 5中的列表框项目传递Id

Asp.net mvc 无法从mvc 5中的列表框项目传递Id,asp.net-mvc,listbox,asp.net-ajax,Asp.net Mvc,Listbox,Asp.net Ajax,我已经为按钮单击时列表框中的多个值编写了代码。我使用自动完成文本框来获取所需的值及其id。然后我设法将这些值添加到列表框中。现在我要做的是将id传递给listbox中的那些值,而不是名称。下面是我正在运行的代码 StudentBatch.cs Create.cshtml StudentController.cs 如果我理解正确的话,这里的问题是将多个ID从列表框传递到POST?一种可能的方法是在控制器中这样分配viewbag: ViewBag.StudentList = new SelectLi

我已经为按钮单击时列表框中的多个值编写了代码。我使用自动完成文本框来获取所需的值及其id。然后我设法将这些值添加到列表框中。现在我要做的是将id传递给listbox中的那些值,而不是名称。下面是我正在运行的代码

StudentBatch.cs

Create.cshtml

StudentController.cs


如果我理解正确的话,这里的问题是将多个ID从列表框传递到POST?一种可能的方法是在控制器中这样分配viewbag:

ViewBag.StudentList = new SelectList(db.StudentBatch, "studentId", "StudentName");
我使用实体来访问我的模型,我认为您有一点不同的方法,但重点是为Selectlist提供一个集合。然后指定数据值字段和数据文本字段

然后在您的视图中,请注意它不是ListBoxFor,因为它是动态的:

@Html.ListBox("StudentList", null)
最后,为了能够接收POST中的值,将以下参数添加到您的操作中,您应该能够对其进行迭代:

string[] StudentList

最后设法从列表框中获取相关的。我遵循了一个特别问题中提到的指示。我就是这样做的

 $("#StudentName").autocomplete({
    source: function (request, response) {
        $.ajax({
            cache: false,
            // async: false, NEVER do this
            url: '@Url.Action("CreateStudent", "Student")', // don't hard code your url's
            data: { Prefix: request.term }, // change
            dataType: "json",
            type: "POST",
            // contentType: "application/json; charset=utf-8", delete
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.label,
                        id: item.id
                    }
                }));
            }
        })
    },select: function(event, ui) {
        $.ajax({
            cache: false,
            type: "POST",
            url: '@Url.Action("GetDetails", "Student")',
            data: { id: ui.item.id },
            success: function (data) {
                var name = $("#StudentName").val();
                $('#SelectedNames').
               append($("<option></option>").
               attr("value", ui.item.id).
               text(name));
                $('#hiddensq').append("<input name='UserValues' type='hidden' value='" + ui.item.id + "'/>");

            }
        });
    }
});
</script>

提交表单时,所选选项将发送到服务器。这意味着,用户必须在列表框中选择一项。如果要发送Id,请将Id设置为选项的值属性值。如果需要,可以在列表框中选择一个或多个值。例如:$'SelectedNames'.val[Aaa,Bbb];所以如果我写$'SelectedNames'.val[Id,FirstName];它在列表框中返回[object]!。如果要选择项目Aaa和Bbb多个选项,请使用val方法进行选择。已从列表框中选择了多个项目。我想说的是,要在列表框中显示的名称及其所需的id将在控制器post操作方法中传递。所以如果我写return{label:item.Id,Id:item.Id};这就是我获取id的方式,但我不想显示它。那么如何从列表框中访问所有id。您需要将id设置为选项的值属性值,并且在提交表单时,所选选项id将被提交。这不是您所解释的。单击按钮时,自动完成文本框值存储在列表框中。
@Html.ListBox("StudentList", null)
string[] StudentList
 $("#StudentName").autocomplete({
    source: function (request, response) {
        $.ajax({
            cache: false,
            // async: false, NEVER do this
            url: '@Url.Action("CreateStudent", "Student")', // don't hard code your url's
            data: { Prefix: request.term }, // change
            dataType: "json",
            type: "POST",
            // contentType: "application/json; charset=utf-8", delete
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.label,
                        id: item.id
                    }
                }));
            }
        })
    },select: function(event, ui) {
        $.ajax({
            cache: false,
            type: "POST",
            url: '@Url.Action("GetDetails", "Student")',
            data: { id: ui.item.id },
            success: function (data) {
                var name = $("#StudentName").val();
                $('#SelectedNames').
               append($("<option></option>").
               attr("value", ui.item.id).
               text(name));
                $('#hiddensq').append("<input name='UserValues' type='hidden' value='" + ui.item.id + "'/>");

            }
        });
    }
});
</script>
[HttpPost]


   public JsonResult GetDetails(string id)
    {

        ViewBag.Value = id;

        return Json(ViewBag.Value, JsonRequestBehavior.AllowGet);
    }