0

I am trying to get the value of the DropDownList of my view .

I send my collection in each DDL using viewbag like this :

 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();
    }

So the create view code :

@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")
}

So when i click on the save button all ids are null .But i want to get the value of selected items in DDL .

You can see my create action after postback:

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

How can i do that ? Best regards

1

1 Answer 1

1

Since you have @Html.DropDownList("ClassName") in your controller on postback you must make the controller parameter (int ClassName). You can also do this.

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

The dropdownlist will bind to your model class called ClassID You will not be able to post the textual value of the ddl to the controller, only the ID behind the ddl

Sign up to request clarification or add additional context in comments.

1 Comment

So i changed my code ,but i got several errors in my view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.