Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取MVC中下拉列表的值_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 获取MVC中下拉列表的值

C# 获取MVC中下拉列表的值,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我正在尝试获取我视图的DropDownList的值 我使用viewbag在每个DDL中发送我的收藏,如下所示: public ActionResult Create() { ClassRepository objclassrep = new ClassRepository(); DegreeRepositor objdegreerep=new DegreeRepositor(); FacultyRepositor objfactulyre

我正在尝试获取我视图的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")
}
我该怎么做?
致以最诚挚的问候

由于回发时控制器中有
@Html.DropDownList(“ClassName”)
,因此必须设置控制器参数
(int ClassName)
。你也可以这样做

 @Html.DropDownListFor(x => x.ClassID, (SelectList)ViewBag.ClassName);
dropdownlist将绑定到名为ClassID的模型类
您将无法将ddl的文本值发布到控制器,只有ddl后面的ID

可以在这里看到我的答案,因此我更改了代码,但我的视图中出现了几个错误。
 @Html.DropDownListFor(x => x.ClassID, (SelectList)ViewBag.ClassName);