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