1

ビューのドロップダウン リストに、db から入力された 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; }

}

前もって感謝します。

4

1 に答える 1

1

次のようなドロップダウンへのデータソースとして適切なSelectList(あなたの場合はViewBagsを介して)供給してください:

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

等々...

于 2013-10-03T06:06:49.683 に答える