3

For whatever reason, my default value for my Html.DropDownListFor isn't working:

@Html.DropDownListFor(model => model.DomainList,
    new SelectList(Model.DomainList, "DomainId", "Name", Model.SingleDomain.DomainId),
    new { @class = "form-control" })

Any idea why?

UPDATE

With the answer below, I updated my code to the following:

@Html.DropDownListFor(model => model.SelectedDomain, 
    new SelectList(Model.DomainList, "DomainId", "Name", Model.SelectedDomain),
    "Select a Domain", 
    new { @class = "form-control" })
CIA
  • 302
  • 1
  • 5
  • 16
  • Is the value of Model.SingleDomain.DomainId in the list of values in model.DomainList? – Jack Jan 12 '14 at 21:44
  • Yes. Even if I input a value I know exists instead of that variable, the dropdownlist doesn't set the default value. – CIA Jan 13 '14 at 03:32
  • 1
    Also, generally when you do a DropDownListFor, it is for an int (to store the selected value), not the List itself. Meaning it should read something like: @Html.DropDownListFor(x => x.DomainId, ....) – Jack Jan 13 '14 at 05:40
  • Sure, but any clue as to why the default selected item won't work? – CIA Jan 16 '14 at 00:59
  • Did you try my last comments suggestion? – Jack Jan 16 '14 at 03:15
  • You are binding your Drop Down Lists selected item to a list which does not make sense. You need to bind it to an int id. – Jack Jan 16 '14 at 03:29

3 Answers3

8

Try this:

In your ViewModel, add an integer to store the selected domain id:

public int SelectedDomainId { get; set; }

Change your DropDownListFor to:

@Html.DropDownListFor(model => model.SelectedDomainId,
    new SelectList(Model.DomainList, "DomainId", "Name", Model.SingleDomain.DomainId),
    new { @class = "form-control" })

Now on your post-back the selected id will be posted in SelectedDomainId.

Or, you could add the single domain object to your VM (to indicate which one was selected):

public Domain SelectedDomain { get; set; }

And change your DropDownListFor to:

@Html.DropDownListFor(model => model.SelectedDomain,
        new SelectList(Model.DomainList, "DomainId", "Name", Model.SingleDomain.DomainId),
        new { @class = "form-control" })

I usually use the selected ID, but I think either should work

Here is another good example:

MVC3 DropDownListFor - a simple example?

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
0

If someone is having trouble with Html.DropdownList keep in mind that

@Html.DropDownList("Country", ViewBag.Country as List<SelectListItem>) --> doesn't select the default value

@Html.DropDownList("Countries", ViewBag.Country as List<SelectListItem>) --> selects the default value

For more details, see: http://www.binaryintellect.net/articles/69395e7d-7c7c-4318-97f5-4ea108a0da97.aspx

Mateut Alin
  • 1,173
  • 4
  • 17
  • 34
0

my problem solved when change viewbag.xxxxxxx to viewbag.xxxxxxx1 issue was hapend when model propety and viewbag property was same.

controller :

 ViewBag.serviceid = new SelectList(db.services.ToList(), "id", "titel", package.service_id);

view:

     @Html.DropDownListFor(Model => Model.service_id, ViewBag.serviceid as SelectList, htmlAttributes: new { @class = "form-control" })