I have a DropDownListFor bound to a model member as well as a list of items to choose from. The binding to this member works, but what I can't seem to figure out is how to display the current value of the model when the page loads.
View
@Html.DropDownListFor(model => model.MethodofPayment, (List<SelectListItem>)ViewBag.statuses)
Control
List<SelectListItem> statuses = new List<SelectListItem>();
statuses.Add(new SelectListItem { Text = "Approved", Value = "Approved" });
statuses.Add(new SelectListItem { Text = "Declined", Value = "Declined" });
statuses.Add(new SelectListItem { Text = "Pending", Value = "Pending" });
ViewBag.ClaimStatus = statuses;
Adding the optionLabel argument doesn't work and returns a null value, and passing in a SelectListItem as the optionLabel parameter doesn't work either.
@Html.DropDownListFor(model => model.MethodofPayment, (List<SelectListItem>)ViewBag.methodofpayment, model.MethodOfPayment)
I want the page to submit the same value shown if they don't change the list item.
Edit:
Thanks you guys it must have been a name conflict.
I messed up asking my question a bit cause I have two drop down lists and I must have copied the wrong one.
The Changed Code:
Controller:
List<SelectListItem> statuses = new List<SelectListItem>();
statuses.Add(new SelectListItem { Text = "Approved", Value = "Approved" });
statuses.Add(new SelectListItem { Text = "Declined", Value = "Declined" });
statuses.Add(new SelectListItem { Text = "Pending", Value = "Pending" });
ViewBag.ClaimStatuses = statuses;
View:
@Html.DropDownListFor(model => model.ClaimStatus, (List<SelectListItem>)ViewBag.ClaimStatuses)
Made it plural and it works.
MethodofPaymentin the model before model binding to the view or it's null? - Dennis R