【问题标题】:DROP DOWN LIST SENDING ENTITY UNIQUE ID INSTEAD OF SELECTED ITEM下拉列表发送实体唯一 ID 而不是所选项目
【发布时间】:2013-10-04 13:41:44
【问题描述】:

我正在我的视图中填充一个下拉列表,其中包含从我的数据库填充的 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; }

}

提前谢谢你。

【问题讨论】:

    标签: asp.net asp.net-mvc-4


    【解决方案1】:

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

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

    等等……

    【讨论】:

    • 嗨.. 感谢您的回复.. 不,它仍然无法正常工作.. 有趣的是,我上面的相同逻辑在另一个控制器/视图关系中完美运行。再次感谢
    • 我无法重现您的问题(使用两种绑定:显式和基于 mvc 约定)。我无法获得您的陈述“..它传递该实体的唯一 ID 而不是选定的值”。现在取决于您的数据库插入逻辑。你能显示 Create() 的 POST 版本吗?
    • @Bishnu ... 这是我的一个愚蠢的错误。我花了半天时间才发现,我实际上是在调用 Id 值而不是我想要的字段值。 . 但是谢谢一百万..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多