1

I have used a ViewBag to pass a list of values from controller to be set in a dropdownlist in a Razor view.

In Create

Controller:

List<SelectListItem> status_list = new List<SelectListItem>();
status_list.Add(new SelectListItem() { Text = "Closed", Value = "Closed" });
status_list.Add(new SelectListItem() { Text = "Open", Value = "Open" });
ViewBag.closed_status = new SelectList(status_list, "Value", "Text");

View:

<div class="form-group input-group">
    <span class="input-group-addon" style="width: 200px">Closed Status</span> 
    @Html.DropDownList("closed_status", (SelectList)ViewBag.closed_status, "Select", new { @class = "form-control", @style = "width: 200px", required = "required" })
</div>

I have used the same coding in Edit View/controller also.

I want display the value selected during creating in the edit mode also. But it shows the default value which is "select".

Can you please suggest me a way of displaying the value?

Werner
  • 2,126
  • 2
  • 22
  • 34
  • You cannot use the same property for the `SelectList` and the property you bind to. And creating a second identical `IEnumerable` fro the first one using `new SelectList(...)` is pointless extra overhead –  Oct 25 '17 at 10:57

0 Answers0