1
我在我的視圖中填充了一個下拉列表,其中包含從我的數據庫填充的view.bag發送的項目。在視圖中一切都很好,但是當它保存到數據庫時,它傳遞的是該實體的唯一ID而不是選定的值。是否有人可以幫助... 謝謝...刪除列表發送實體唯一標識不包含所選項目
* note my static填充下拉工作...它從數據庫的下拉列表導致proplem。
繼承人我的代碼:
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; }
}
預先感謝您。
嗨..感謝您的答覆。沒有它仍無法正常工作..有趣的是,上面的同樣的邏輯在另一個控制器/視圖關係中完美工作。再次感謝 – digiShadoe
我無法重現您的問題(使用兩種綁定:基於顯式和mvc約定)。我無法得到你的陳述「......它沿着該實體的唯一ID傳遞,而不是選定的值」。它現在取決於你的數據庫插入邏輯。你能顯示發佈版本的Create()嗎? –
@Bishnu ...這是我的一部分愚蠢的錯誤..我花了半天的時間來發現,在我看來,我實際上是調用Id值而不是我想要的字段值..但是,萬分感謝.. – digiShadoe