1

I'm populating a drop down list in my view with items sent from a view.bag populated from my db. In the view everything is fine, but when it saves to the database its passing along the unique ID for that entity instead of the selected value.. Can someone please help.. Thank You...

*note my static populated dropdown works... Its the dropdown from the database thats causing the proplem.

Heres my code:

 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();
    }

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>

and model:

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; }

}

thank you in advance.

4

1 回答 1

1

将适当SelectList的(在您的情况下通过 ViewBags)作为数据源提供给下拉列表,例如:

@Html.DropDownList("CourseSection", (SelectList)ViewBag.CourseID)
@Html.DropDownList("TeacherID", (SelectList)ViewBag.TeacherID )

等等...

于 2013-10-03T06:06:49.683 回答