I’ve found tons of info on dropdown list but still seem to have problems with mine... When my user selects a value from the dropdown list then click on save. Everything
else but the selected value from the dropdown list show in the resulting view. How can I fix this?
Here’s my view:
<td>Course @Html.DropDownList("CourseId")</td>
Here’s my controller:
public ActionResult Create()
{
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
}
[HttpPost]
public ActionResult Create(CourseProgress courseprogress)
{
if (ModelState.IsValid)
{
db.CourseProgresses.Add(courseprogress);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.Course.CourseId);
where you need to add selectedId as a property in your module(CourseProgress), and you can access the value of it from your Create method with HTTPOST attribute.
AlexanderBla...
Member
325 Points
309 Posts
Dropdown List values not being saved... how can I fix this?
Feb 19, 2012 11:37 PM|LINK
I’ve found tons of info on dropdown list but still seem to have problems with mine... When my user selects a value from the dropdown list then click on save. Everything else but the selected value from the dropdown list show in the resulting view. How can I fix this?
Here’s my view:
<td>Course @Html.DropDownList("CourseId")</td>
Here’s my controller:
public ActionResult Create()
{
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
}
[HttpPost]
public ActionResult Create(CourseProgress courseprogress)
{
if (ModelState.IsValid)
{
db.CourseProgresses.Add(courseprogress);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", courseprogress.Course.CourseId);
return View(courseprogress);
}
Thanks for any help
bmatt
Member
103 Points
27 Posts
Re: Dropdown List values not being saved... how can I fix this?
Feb 19, 2012 11:49 PM|LINK
try to use
@Html.DropDownListFor(model => model.SelectedId, (SelectList)ViewBag.CourseId)
where you need to add selectedId as a property in your module(CourseProgress), and you can access the value of it from your Create method with HTTPOST attribute.
AlexanderBla...
Member
325 Points
309 Posts
Re: Dropdown List values not being saved... how can I fix this?
Feb 21, 2012 03:25 AM|LINK
I tried all three of these and none worked.
<td>Course @Html.DropDownListFor(model => model.SelectidId, (SelectList)ViewBag .CourseId)</td>
<td>Course @Html.DropDownListFor(model => model.CourseId, (SelectList)ViewBag .CourseId)</td>
<td>Course @Html.DropDownListFor(model => model.Course.CourseId , (SelectList)ViewBag .CourseId)</td>
Perhaps it’s because I’m using complex types like these:
<td>Student @Html.EditorFor(model => model.Student.Name)</td>
<td>Teacher @Html.DropDownList("TeacherId", String.Empty)</td>
<td>Start Date @Html.EditorFor(model => model.PdfReport.StartDate)</td>
I’m still researching but so far nothing I found works for me.
bruce (sqlwo...
All-Star
36882 Points
5451 Posts
Re: Dropdown List values not being saved... how can I fix this?
Feb 21, 2012 04:00 AM|LINK
you should use @Html.DropDownListFor.
what is the property name and type in CourseProgress that the dropdown is supoosed to bind to?
AlexanderBla...
Member
325 Points
309 Posts
Re: Dropdown List values not being saved... how can I fix this?
Feb 21, 2012 06:21 AM|LINK
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Dropdown List values not being saved... how can I fix this?
Feb 21, 2012 05:13 PM|LINK
See Working with the DropDownList Box and jQuery and My blog Cascading DropDownList in ASP.Net MVC