我在我的视图中填充了一个下拉列表,其中包含从我的数据库中填充的view.bag发送的项目。在视图中一切都很好,但当它保存到数据库时,它传递该实体的唯一ID而不是选定的值..有人可以帮助.. 谢谢......
*请注意我的静态填充下拉列表...它是数据库中的下拉列表导致问题。
继承我的代码:
Controller:
public ActionResult Create()
{
ViewBag.TeacherID = new SelectList(db.Teachers, "TeacherID", "LastName");
ViewBag.SemesterID = new SelectList(db.Semesters, "SemesterID",
SemesterName");
ViewBag.CourseID = new SelectList(db.Courses, "Section", "CourseSection");
ViewBag.RoomID = new SelectList(db.Rooms, "RoomID", "FacilityIdentifier");
var meetlist = new SelectList(new[]
{
new { ID="M", Name="M"},
new { ID="T", Name="T"},
new { ID="W", Name="W"},
new { ID="TH", Name="TH"},
new { ID="T", Name="T"},
new { ID="F", Name="F"},
new { ID="MW", Name="MW"},
new { ID="TTH", Name="TTH"},
},
"ID", "Name", 1);
ViewData["meetlist"] = meetlist
return View();
}
查看:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Assignment</legend>
<div class="editor-label">
COURSE
</div>
<div class="editor-field">
@Html.DropDownList("CourseSection", String.Empty)
@Html.ValidationMessageFor(model => model.Course.Section)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TeacherID)
</div>
<div class="editor-field">
@Html.DropDownList("TeacherID", String.Empty)
@Html.ValidationMessageFor(model => model.TeacherID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.RoomID)
</div>
<div class="editor-field">
@Html.DropDownList("RoomID", String.Empty)
@Html.ValidationMessageFor(model => model.RoomID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SemesterID)
</div>
<div class="editor-field">
@Html.DropDownList("RoomID", String.Empty)
@Html.ValidationMessageFor(model => model.RoomID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EnrollCap)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EnrollCap)
@Html.ValidationMessageFor(model => model.EnrollCap)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EnrollTotal)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EnrollTotal)
@Html.ValidationMessageFor(model => model.EnrollTotal)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.StartTime)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.StartTime)
@Html.ValidationMessageFor(model => model.StartTime)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EndTime)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EndTime)
@Html.ValidationMessageFor(model => model.EndTime)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Meeting)
</div>
<div>
@Html.DropDownListFor(u => u.Meeting, ViewData["meetlist"] as SelectList,
new { style = "width: 200px"})
@Html.ValidationMessageFor(u=>u.Meeting)
</div>
<p>
<input type="submit" value="Create" />
</p>
和型号:
public class Assignment
{
public int AssignmentID { get; set; }
public int CourseID { get; set; }
public int TeacherID { get; set; }
public int RoomID { get; set; }
public int SemesterID { get; set; }
public int EnrollCap { get; set; }
public int EnrollTotal { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string Meeting { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Room> Room { get; set; }
public virtual ICollection<Teacher> Teacher { get; set; }
public virtual ICollection<Semester> Semester { get; set; }
}
提前谢谢。
答案 0 :(得分:1)
将适当的SelectList
(通过您的案例中的ViewBags)作为数据源提供给下拉列表:
@Html.DropDownList("CourseSection", (SelectList)ViewBag.CourseID)
@Html.DropDownList("TeacherID", (SelectList)ViewBag.TeacherID )
依旧......