Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.dropdownlist does not display the default/selected value in the dropdown field

I have a table with various rows and each row corresponds to an ID. For every row, I have EDIT option which when selected should enable the user to look at the default values corresponding to that ID in the 'Edit.chtml' fields and there by update it by erasing the default values and typing in the new entry and saving it.

In 'Edit.chtml', I have a field for "MANAGERS" that I need to populate with a dropdownlist of names with default/selected value displayed corresponding to the ID of the row where Edit operation is selected from the table. I am using viewBag but my problem is the dropdownlist is always displaying the first item in the list and not the selected value. Please see the code below.

Controller:
 //
        // GET: /ManagersNAMES/Edit/5

        public ActionResult Edit(int id)
        {
            using (var db = new InpEntities())
            {
                var list_managers = (from x in db.TABLE_MANAGERS
                                        select new TABLE_MANAGERSDTO
                                        {
                                            ID = x.ID,
                                            NAME = x.NAME,
                                        }).OrderBy(w => w.NAME).ToList();

                ViewBag.managers = new SelectList(list_managers, "ID", "NAME", id);

                return View();
            }
        }

        public class TABLE_MANAGERSDTO
        {
            public string NAME { get; set; }
            public int ID { get; set; }
        }

        //
        // POST: /ManagersNAMES/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, TABLE_INSTITUTES dp)
        {
            try
            {

                using (var db = new InpEntities())
                {

                    TABLE_INSTITUTES tmp = (TABLE_INSTITUTES)db.TABLE_INSTITUTES.Where(f => f.ID == id).First();
                    tmp.MANAGER = dp.MANAGER;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }

View: 'Edit.chtml'

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>

    <div class="editor-label" style="font-weight:bold">MANAGER</div>
    <div class="editor-field">
        @Html.DropDownList("dp.MANAGER", (SelectList)ViewBag.managers)
    </div>

    <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
like image 486
sunskin Avatar asked Jun 10 '13 14:06

sunskin


People also ask

How to set default value of dropdown HTML?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.

How do I add a default item to DropDownList?

The default item is added to the DropDownList by placing a ListItem and setting the AppendDataBoundItems property to True.

What is HTML DropDownList?

DropDownList(String, String, IEnumerable<SelectListItem>, Object, Object) Returns an HTML drop-down list control that has the specified name, custom attributes defined by an attribute object, and default selection, and that contains the specified list items and default item.


2 Answers

You need to set the selected id within the controller like this:

var list_managers =  list_managers.Select(x => new
                          {
                              Value = x.ID.ToString(),
                              Text = x.Name
                          })
                          .ToList();

ViewBag.managers = new SelectList(list_managers, "ID", "NAME", id);

And change your view to:

@Html.DropDownList("dpMANAGER", (SelectList)ViewBag.managers)
like image 95
hutchonoid Avatar answered Oct 16 '22 10:10

hutchonoid


Just wrote @Html.DropDownList("dp.MANAGER", ViewBag.managers) without selectlist, becouse its already in type of SelectList.

And write ViewBag.managers = new SelectList(list_managers, "ID", "NAME"); like this

like image 38
Timur Shahbanov Avatar answered Oct 16 '22 11:10

Timur Shahbanov