如何访问控制器中视图下拉列表的值?
var typeList = from e in db.Rubrics where e.DepartmentID == 2 select e;
var selectedRubrics = typeList.Select(r => r.Category);
IList <String> rubricsList = selectedRubrics.ToList();
IList<SelectListItem> iliSLI = new List<SelectListItem>();
SelectListItem selectedrubrics = new SelectListItem();
selectedrubrics.Text = "Choose a category";
selectedrubrics.Value = "1";
selectedrubrics.Selected = true;
iliSLI.Add(selectedrubrics);
for(int i = 0;i<rubricsList.Count();++i)
{
iliSLI.Add(new SelectListItem() {
Text = rubricsList[i], Value = i.ToString(), Selected = false });
}
ViewData["categories"] = iliSLI;
在我看来,这可以很好地显示下拉值:
@Html.DropDownList("categories")
然后在我的控制器中我这样使用FormCollection
:
String[] AllGradeCategories = frmcol["categories"].Split(',');
当我在这里设置断点时,我会在 AllGradeCategories中得到一个 1 数组。我究竟做错了什么?
更多:这是我的开始表格:
@using (Html.BeginForm("History", "Attendance",
new {courseID = HttpContext.Current.Session ["sCourseID"] },
FormMethod.Post, new { @id = "formName", @name = "formName" }))
{
<td>
@Html.TextBoxFor(modelItem => item.HomeworkGrade, new { Value = "7" })
@Html.ValidationMessageFor(modelItem => item.HomeworkGrade)
</td>
<td>
@Html.TextBoxFor(modelItem => item.attendanceCode, new { Value = "1" })
@Html.ValidationMessageFor(model => model.Enrollments.FirstOrDefault().attendanceCode)
</td>
<td>
@Html.EditorFor(modelItem => item.classDays)
</td>
<td>
@Html.DropDownList("categories")
</td>
}
我的控制器签名:
public ActionResult ClassAttendance(InstructorIndexData viewModel, int id, FormCollection frmcol, int rows, String sTeacher, DateTime? date = null)
EDITED 2 试过这个,但虽然它似乎已发布,但我仍然没有得到隐藏字段或类别参数中列表的值。
@Html.Hidden("dropdownselected1");
@Html.DropDownList("categories",ViewBag.categories as SelectList, new { onchange = "changed" })
</td>
$(function () {
$("#dropdownselected1").val($("#categories").val());
});