在我的MVC4项目中,我无法在编辑控制器上获取我的DropDownList数据。 我的UI语法是波纹管:为什么我在提交事件时未能获得DropDownList值
<div class="form-group">
@Html.HiddenFor(model => model.School.SchoolID)
@Html.LabelFor(model => model.School.SchoolName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.School.SchoolName)
@Html.ValidationMessageFor(model => model.School.SchoolName)
</div>
</div>
<div class="form-group">
@Html.HiddenFor(model => model.StudentCLass.ID)
@Html.LabelFor(model => model.StudentCLass.ClassName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.StudentCLass.ID, @ViewBag.StudentCLassList as SelectList,"Select Class")
@Html.ValidationMessageFor(model => model.StudentCLass.ID)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
为了填补DropDownList的我用波纹管语法:点击提交按钮
public ActionResult Edit(int Id)
{
using (DB = new StudentContext())
{
var result = DB.Students.FirstOrDefault(c => c.ID == Id);
ViewBag.StudentCLassList = new SelectList(DB.StudentClasses
.Select(sc => new ViewModelClass
{
ID = sc.ID,
ClassName = sc.ClassName
}).ToList(), "ID", "ClassName");
return View(StudentInfo(result));
}
}
之后,我不能让我的控制器操作的DropDownList值。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ViewModel.ViewModelStudents student)
{
var tempResult = student.StudentCLass.ID;
//return RedirectToAction("Index");
// return查看(学生); }
模型结构
public partial class StudentClass
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public StudentClass()
{
Students = new HashSet<Student>();
}
public int ID { get; set; }
[StringLength(100)]
public string ClassName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Student> Students { get; set; }
}
显示您的模型类 –