triggermouse
triggermouse

Reputation: 143

MVC4 EF cannot get saved value to display in dropdown on page load

I have never asked a question on StackOverflow before, and never wanted to, but I am desperate, so here we go: I cannot get a saved value to show up as the default value/display in a dropdown.

I set up the list in my controller:

public ActionResult Index()
{
    //User Dropdown List
    var users = Roles.GetUsersInRole("Manager");
    SelectList list = new SelectList(users);
    ViewBag.Users = list;

    return View();
}

Then in the view an admin can then select one of these users and save it to my database via EF:

@Html.DropDownList("Users", ViewBag.Users as SelectList, "--Select Manager--")

This all works great, however, when you edit this entry, I want the dropdown list to show the current saved manager, not the first name in the list. I was hoping on my edit action that I could pull the current manager out of the database and pass it back into the dropdown as the default selected item, but no go:

public ActionResult Edit(int id = 0)
{
    var theOwner = (from v in _db.Location where v.LocationID == id select v.Owner).FirstOrDefault();

    var users = Roles.GetUsersInRole("Manager");
    SelectList list = new SelectList(users, theOwner);
    ViewBag.Users = list;

From all the examples I have read over the last 2 weeks, everyone has had 3 different values to work within their dropdowns, making it possible to use all the overloads in the SelectList method. However, my problem is that I just have this string list with only one item in it, so I can't utilize the overloads as I want.
So does anyone have an idea on how I can get this to work? Thanks a lot in advance for your time on this!

Upvotes: 0

Views: 580

Answers (2)

David Tansey
David Tansey

Reputation: 6023

I'm pretty sure that if you modify the second parameter on the line where you create your SelectList, it should work -- it does for me.

Here is what I think the trouble is: Currently you are specifying the second parameter as 'theOwner', which is an object reference from the earlier Linq statement. But the SelectList contains a bunch of strings (the UserNames of the users which match the specified rolename). As a result, the SelectList doesn't 'know' how to match what you specified as the SelectedItem to something in the list of strings it contains.

But if you refine that second parameter so it specifies the USERNAME of the Owner that you just looked up, it should work. However I do not know what the correct property name is from your Location table. If the field you are currently selecting (v.Owner) contains the UserName itself rather than some Key then the syntax would be:

SelectList list = new SelectList(users, theOwner.Owner);

If that column actually contains a key for the User like an int or a Guid then you will have query for the UserName using the key, but the nature of the fix is the same.

Hope that helps.

Upvotes: 1

Dejan Janjušević
Dejan Janjušević

Reputation: 3230

A quick workaround is not to use @Html.DropDownList but plain html code.

As an example for your case, use the following html code in your View instead of Html.DropDownList helper:

<!-- NOTE: the ID and name attributes of "select" tag should be the same as 
the name of the corresponding property in your Model in order for ASP.NET MVC
to edit your Model correctly! --> 
<select id="User" name="User">    
@foreach (var user in (SelectList)ViewBag.Users)
{
    if (user == ViewBag.TheOwner)
    {
        <option value="@user" text="@user" selected = "selected" />
    }
    else
    {
        <option value="@user" text="@user" />
    }
}
</select>

Also , for this to work you need to add one more line to your Edit method:

ViewBag.TheOwner = theOwner;

Another solution is also possible using @Html.DropDownListFor() however you haven't shown your model so I can't tell you what exactly to use. When DropDownListFor is used, ASP.NET MVC will select an option automatically based on the value in your model.

Upvotes: 0

Related Questions