簡體   English   中英

獲取MVC中的下拉列表的值

[英]Get the value of Drop Down List in MVC

我正在嘗試獲取我的view的DropDownList的值。

我使用如下viewbag在每個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均為null。但是我想獲取DDL中所選項目的值。

回發后,您可以看到我的create動作:

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

我怎樣才能做到這一點 ? 最好的祝福

由於回發時控制器中有@Html.DropDownList("ClassName") ,因此必須設置控制器參數(int ClassName) 您也可以這樣做。

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

下拉列表將綁定到名為ClassID的模型類。您將無法將ddl的文本值發布到控制器,只能將ddl后面的ID發布到控制器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM