【发布时间】:2021-06-14 11:06:20
【问题描述】:
我在 cshtml 中使用 select2 创建了一个下拉列表,它允许从下拉列表中选择多个数据。
SelectList studentList = new SelectList(Model.CourseStudents.Select(_ => _.Student).OrderBy(_ => _.DisplayName), "Id", "DisplayName", null);
<div>
<div class="form-group row-5 col-2">
<label asp-for="AssignmentStudents">Students</label>
<select data-placeholder="Select specific students. Leave empty for all students" asp-for="AssignmentStudents" id="UserId" name="UserId" asp-items="studentList" multiple class="js-select2">
</select>
</div>
</div>
上面的代码在表单中创建了一个页面,从下拉列表中选择值后,选项中的用户 ID 转到控制器并保存到数据库中,这很好,但是当我重定向时我该怎么做到我需要在提交表单时选择的下拉列表中显示名称的页面我该怎么做?
例如:-
在提交表单时,假设我选择了两个名称 A 和 B,其 ID 分别为 1 和 2。在控制器中,在提交表单时,model.AssignmentStudents 中的值变为1,2,但在重定向到表单时使用相同的值,名称不会显示在下拉列表中。我该怎么做?
【问题讨论】:
-
嗨@Biraj Dahal,关于这个案例有什么更新吗?
标签: c# drop-down-menu asp.net-core-mvc razor-pages