我试图在表单内部动态创建下拉列表,并将所有下拉列表和其他表单控件的选定值返回给控制器。
下拉列表的最小数目为1,并且可以存在的最大下拉列表数目为下拉列表中的选项数目。例如,如果选项数量为5,则下拉列表的数量范围可以从1到5。
下拉列表选项
public enum Subject: byte
{
English = 0,
Mathematics,
Biology,
Physics,
Chemistry,
Geography,
History
}
模型
public class Student
{
public int Id { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "D.O.B")]
public string DOB { get; set; }
public string SelectedSubject { get; set; }
//I want to create dynamic drop down lists for subjects
//TODO: add supporting properties to get selected subjects
}
查看
...
@using (Html.BeginForm())
{
<div class="form-horizontal">
@Html.HiddenFor(model => model.Id)
<div class="form-group>
@Html.LabelFor(model => model.Name)
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
</div>
</div>
<div class="form-group>
@Html.LabelFor(model => model.DOB)
<div class="col-md-10">
@Html.EditorFor(model => model.DOB)
</div>
</div>
<div class="form-group>
@Html.LabelFor(model => model.DOB)
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedSubject, new SelectList(Enum.GetValues(typeof(Subject))))
</div>
</div>
@*Add "Add subject" button"*@
@*Add dropdown list, Add "Remove" button and shift Add button to the back*@
@*the newly added dropdown list is just beside the previous one*@
@*Number of dropdown list: 1 to 7*@
@*If number of dropdown list is 7, add button is hidden*@
@*If number of dropdown list is 1, delete button is hidden*@
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value=Create />
</div>
</div>
</div>
}
控制器
//TODO: add more parameters to support getting the data from the dropdownlist
[HttpPost]
public ActionResult Create(Student student, **add other parameter to support dropdown list selected value**)
{
...
}
所有评论的地方都是我不确定要添加的区域。 预先感谢。