1

I have the DropDownList in view as below,

@Html.DropDownListFor(model => model.RoleID, (IEnumerable<SelectListItem>)ViewBag.RoleID, new { @class = "dropdownlistCustom" })
@Html.DropDownList("RoleID", (IEnumerable<SelectListItem>)ViewBag.RoleID, new { @class = "dropdownlistCustom" })
@Html.ValidationMessageFor(model => model.RoleID)

Tried both DropDownList and DropDownListFor, both are not getting the correct SelectedValue.

From Controller, I am setting the selectedValue param (see Last parameter of SelectList()) .

public ActionResult Edit(int id = 0)
{
    UserDetail userDetail=db.UserDetails.Find(id);
    if(userDetail!=null)
    {
       ViewBag.RoleID = new SelectList(db.Roles.Where(r => r.RoleStatus == "A"), "RoleID", "RoleName", userdetail.RoleID);
       return View(userdetail);
    }
}

Model :

[Display(Name = "Name Of the Role")]
public int RoleID { get; set; }
[ForeignKey("RoleID")]
public virtual Role Roles { get; set; }
Sreekumar P
  • 5,900
  • 11
  • 57
  • 82

2 Answers2

1

First of all I would encourage you not to use ViewBag, but view models to pass your select list (or data for select list) along from controllers to views.

Second of all, I would encourage you to look at this example or this example (preferably both) of DropDownListFor usage. I think you over-complicating SelectList. Less simple it is, less confused you will be as to why is it not working as expected. Just take existing example and change variable names to yours.

Another big tip why not to use ViewBag is that when you build your view model you can build a select list right there in the controller where you fetch your data from wherever. Then in the view you will only need to use pre-built view model property where SelectList argument is provided - cleaner and nicer. View should do least manipulations with data, but only to present it in the right format in the right way in the right place.

Just look at those examples I provided, I am sure you smart enough to figure it out how to use DropDownListFor properly, its very very easy, you just need to provide right parameters at the right places, see what others do and do the same.

Hope this helps, please keep me posted about your progress.

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43
  • 1
    You can, its bad practice. Its like global variables in old days. It is easy to setup and start with, it bytes you back when you need to return to your code, from readability, maintainability perspective. Also not less improtant - its not strongly typed, which is another source of possible bugs. With View Model you have strongly typed properties that can be used for rendering select list, string[] or List for example, but can be anything. Hope it clarifies. – Display Name Mar 11 '13 at 13:28
0

That's because you're not using DropDownListFor correctly. The function definition you're after is:

DropDownListFor(lambda, selectList, *selectedItem*, htmlAttributes)

RoleId should be passed as the selectedItem parameter, so you're missing the selectList parameter. MVC doesn't create that for you automatically with DropDownListFor, you have to tell it what select list to use by either creating it inline, passing it into ViewBag or setting it on your model and specifying which property to use.

EDIT

Sorry. That's my bad. It's still early here and we lost an hour (daylight savings), so my brain is not all there yet. I mixed DropDownListFor and DropDownList. With DropDownListFor, the selected item comes from the set value on the model, but you still need to provide a proper SelectList. It appears you're trying to specify the selected item where you should be telling it what SelectList to use.

If you were to use DropDownList, then you would need to specify both the SelectList to use and the selected value.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • but I can't find a overload which takes `selectedItem` param – Sreekumar P Mar 11 '13 at 13:18
  • I am setting the `SelectList` from the controller, you can see that in my Question, that Edit Action. Also I am setting the SelectedValue as correct value from there (last param)... when I am debugging I can see the correct value is set as selected, but unfortunatly I am not getting that in the View (I also debugged in View rendering area also) – Sreekumar P Mar 11 '13 at 13:32
  • It's probably because you're casting your `SelectList` into an `IEnumerable`. That'll carry your items over, but not the selected value. Cast it as a true-blue `SelectList` and see if that doesn't fix it. – Chris Pratt Mar 11 '13 at 14:57