获取MVC中下拉列表的值

时间:2014-03-24 16:01:39

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

我正在尝试获取我视图的DropDownList的值。

我使用这样的视图包在每个DDL中发送我的收藏:

 public ActionResult Create()
    {
        ClassRepository objclassrep = new ClassRepository();
        DegreeRepositor objdegreerep=new DegreeRepositor();
        FacultyRepositor objfactulyrep=new FacultyRepositor();
        LessonRepository objLessonRep=new LessonRepository();
        MajorRepository objmajorrep=new MajorRepository();
        SemesterRepositor objsemesterrep=new SemesterRepositor();
        TeacherRepositor objteacherrep=new TeacherRepositor();
        ViewBag.ClassName = new SelectList(objclassrep.GetClasslist(), "Id", "ClassName");
        ViewBag.DegreeName = new SelectList(objdegreerep.GetDegreelist(), "Id", "DegreeName");
        ViewBag.FacultyName = new SelectList(objfactulyrep.GetFacultylist(), "Id", "FacultyName");
        ViewBag.LessonName = new SelectList(objLessonRep.GetLessonlist(), "Id", "LessonName");
        ViewBag.MajorName = new SelectList(objmajorrep.GetMajorlist(), "Id", "MajorName");
        ViewBag.TeacherName = new SelectList(objteacherrep.GetTeacherlist(), "Id", "LastName");
        ViewBag.SemesterName = new SelectList(objsemesterrep.GetSemesterlist(), "Id", "SemesterName");





        return View("Create");

        // return View();
    }

创建视图代码:

@model DomainClasses.Schedule

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Schedule</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.TeacherId)
        </div>
        <div class="editor-field">
             @Html.DropDownList("TeacherName") 
            @Html.ValidationMessageFor(model => model.TeacherId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LessonId)
        </div>
        <div class="editor-field">
             @Html.DropDownList("LessonName") 
            @Html.ValidationMessageFor(model => model.LessonId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ClassId)
        </div>
        <div class="editor-field">
            @Html.DropDownList("ClassName") 
            @Html.ValidationMessageFor(model => model.ClassId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DegreeId)
        </div>
        <div class="editor-field">
                @Html.DropDownList("DegreeName") 
            @Html.ValidationMessageFor(model => model.DegreeId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.FacultyId)
        </div>
        <div class="editor-field">
                @Html.DropDownList("FacultyName") 
            @Html.ValidationMessageFor(model => model.FacultyId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.SemesterId)
        </div>
        <div class="editor-field">
                           @Html.DropDownList("SemesterName") 

            @Html.ValidationMessageFor(model => model.SemesterId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.MajorId)
        </div>
        <div class="editor-field">
               @Html.DropDownList("MajorName") 
            @Html.ValidationMessageFor(model => model.MajorId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DateOfExame)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateOfExame)
            @Html.ValidationMessageFor(model => model.DateOfExame)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Capacity)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Capacity)
            @Html.ValidationMessageFor(model => model.Capacity)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.locationOfExame)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.locationOfExame)
            @Html.ValidationMessageFor(model => model.locationOfExame)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

所以当我点击保存按钮时,所有ID都为空。但我想获取DDL中所选项目的值。

您可以在回发后看到我的创建操作:

 [HttpPost]
        public ActionResult Create(Schedule schedule)
        {
            obj.AddNewSchedule(schedule);
            obj.Save();
            return RedirectToAction("Index", "Schedule");
        }

我该怎么做? 最好的问候

1 个答案:

答案 0 :(得分:1)

由于您的回传控制器中有@Html.DropDownList("ClassName"),因此您必须创建控制器参数(int ClassName)。你也可以这样做。

 @Html.DropDownListFor(x => x.ClassID, (SelectList)ViewBag.ClassName);

下拉列表将绑定到名为ClassID的模型类 您将无法将ddl的文本值发布到控制器,只能将ddl后面的ID发布