0

In Controller Method, my code is below

ViewBag.CurrencyNameId = db.CurrencyNames;

In View code in Edit.cshtml is

@Html.DropDownListFor(model => model.CurrencyNameId, new SelectList(ViewBag.CurrencyNameId, "Id", "Name",Model.CurrencyNameId), "Select", new { @class = "form-control" })  

It does not set initial value.

  • First, your cannot use the same name for the property your binding to and the `SelectList` (refer [Will there be any conflict if i specify the viewBag name to be equal to the model property name inside @Html.DropDownListFor](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557). Second, there is no point at all in the 4th parameter of the `SelectList` constructor - its ignored when binding to a model property. –  Feb 25 '17 at 05:14
  • And assuming your `CurrencyNameId` is typeof `int` - then its that value which determines what is selected. Just set its value in the GET method to the value of one of the option values, and it will be selected (after making the changes noted above) - `@@Html.DropDownListFor(m => m.CurrencyNameId, new SelectList(ViewBag.CurrencyList, "Id", "Name), .... )` –  Feb 25 '17 at 05:15
  • Thanks, @Stephen for your answer.It works for me. – Naylin Htike Feb 27 '17 at 03:02

0 Answers0