Reputation: 10689
Below is the Edit
method from my controller. Everything works fine, however the current value for the agent in the FixedOrVariable
drop down is not being set as the default value. I can edit this value properly, however.
The field FixedOrVariable
is a one character field in the model. It's values are either F
or V
(always upper case).
Here is the controller method
public ActionResult Edit(int id)
{
var banklistagentid = db.BankListAgentId.Find(id);
ViewBag.BankID = new SelectList(db.BankListMaster, "ID", "BankName", banklistagentid.BankID);
SelectList tmpList = new SelectList(new[] { "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NA", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "US", "VT", "VI", "VA", "WA", "WV", "WI", "WY" });
ViewBag.StateCodeList = tmpList;
var fixedOrVariable = new[] {new {Text = "Fixed", Value = "F"}, new {Text = "Variable", Value = "V"}};
ViewBag.FixedOrVariable = new SelectList(fixedOrVariable, "Value", "Text", banklistagentid.FixedOrVariable);
return View(banklistagentid);
}
And here is the code from the view
<div class="editor-field">
@Html.DropDownListFor(model => model.FixedOrVariable, (SelectList)ViewBag.FixedOrVariable)
</div>
Upvotes: 0
Views: 304
Reputation: 93434
This is probably the single biggest mistake I've seen people make with Dropdowns in MVC. You cannot name your list of choices the same as your selected item variable.
Change it to ViewBag.FixedOrVariableList
Upvotes: 1