Html.DropdownListFor selected value not being set

Ibrar Hussain

How can I set the selected value of a Html.DropDownListFor? I've been having a look online and have seen that it can be achieved by using the fourth parameter so like the below:

@Html.DropDownListFor(m => m, new SelectList(Model, "Code", "Name", 0),  "Please select a country")

My select list then display like this:

<select id="ShipFromCountries" name="ShipFromCountries">
     <option value="">Please select a country</option>
     <option value="GB">United Kingdom</option>
     <option value="US">United States</option>
     ...
</select>

But for some reason United Kingdom remains selected but I want "Please select a country" to be selected.

Anyone know how I can achieve this?

EDIT

I've updated my code as there was a slight change in functionality however still seem to be encountering this problem. This is what is in my view:

@Html.DropDownListFor(n => n.OrderTemplates, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")

1 is the Id of the option that I want selected, I have also tried with the text of the option but that also does not work.

Any ideas?

Romias

Your code has some conceptual issues:

First,

@Html.DropDownListFor(n => n.OrderTemplates, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")

When using DropDownListFor, the first parameter is the property where your selected value is stored once you submit the form. So, in your case, you should have a SelectedOrderId as part of your model or something like that, in order to use it in this way:

@Html.DropDownListFor(n => n.SelectedOrderId, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")

Second,

Aside from using ViewBag, that is not wrong but there are better ways (put that information in the ViewModel instead), there is a "little bug" (or an unspected behavior) when your ViewBag property, where you are holding the SelectList, is the same name of the property where you put the selected value. To avoid this, just use another name when naming the property holding the list of items.

Some code I would use if I were you to avoid this issues and write better MVC code:

Viewmodel:

public class MyViewModel{
   public int SelectedOrderId {get; set;}
   public SelectList OrderTemplates {get; set;}

   // Other properties you need in your view
}

Controller:

public ActionResult MyAction(){
   var model = new MyViewModel();
   model.OrderTemplates = new SelectList(db.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1);
   //Other initialization code

   return View(model);
}

In your View:

@Html.DropDownListFor(n => n.SelectedOrderId, Model.OrderTemplates, "Please select an order template")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Html.DropDownListFor set selected value

From Dev

MVC DropDownListFor selected value not being selected

From Dev

How to set default selected value for Html.DropDownListFor

From Dev

Using selected value in @Html.DropDownListFor

From Dev

Unable to get @Html.DropDownListFor Selected Value

From Dev

Html.DropDownListFor gives error if no value selected

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Dev

MVC5 Razor html.dropdownlistfor set selected when value is in array

From Dev

@Html.DropDownListFor how to set default value

From Dev

dropdownlistfor selected value with viewdata

From Dev

DropDownListFor selected value not working

From Dev

Set selected value for DropDownListFor in controller does not work in razor view

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

ASP.NET MVC @Html.DropdownListFor wrong selected value

From Dev

DropDownListFor not always showing selected value

From Dev

'dropdownlistfor' controller not taking the selected value

From Dev

How to get value selected in @Html.DropDownListFor inside the view and pass it to action in OTHER controller?

From Dev

MVC DropDownListFor - Selected Value ViewModel Class

From Dev

DropDownListFor with an item value 0 always selected

From Dev

Selected value for dropdownlistfor in mvc4

From Dev

DropDownListFor not setting selected value with different names

From Dev

How to set state of dropdownlistfor default value

From Dev

MVC - DropDownListFor Enum - Set Default Value

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Setting default value to Html.DropDownListFor

From Dev

Wrong value binding with Html.DropDownListFor

From Dev

Get DropDownListFor selected value into Ajax.ActionLink call

From Dev

DropDownListFor<> not using my selected value if I change it in the model before call

Related Related

  1. 1

    Html.DropDownListFor set selected value

  2. 2

    MVC DropDownListFor selected value not being selected

  3. 3

    How to set default selected value for Html.DropDownListFor

  4. 4

    Using selected value in @Html.DropDownListFor

  5. 5

    Unable to get @Html.DropDownListFor Selected Value

  6. 6

    Html.DropDownListFor gives error if no value selected

  7. 7

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  8. 8

    MVC5 Razor html.dropdownlistfor set selected when value is in array

  9. 9

    @Html.DropDownListFor how to set default value

  10. 10

    dropdownlistfor selected value with viewdata

  11. 11

    DropDownListFor selected value not working

  12. 12

    Set selected value for DropDownListFor in controller does not work in razor view

  13. 13

    MVC DropdownlistFor set selected value not working?c#

  14. 14

    ASP.NET MVC @Html.DropdownListFor wrong selected value

  15. 15

    DropDownListFor not always showing selected value

  16. 16

    'dropdownlistfor' controller not taking the selected value

  17. 17

    How to get value selected in @Html.DropDownListFor inside the view and pass it to action in OTHER controller?

  18. 18

    MVC DropDownListFor - Selected Value ViewModel Class

  19. 19

    DropDownListFor with an item value 0 always selected

  20. 20

    Selected value for dropdownlistfor in mvc4

  21. 21

    DropDownListFor not setting selected value with different names

  22. 22

    How to set state of dropdownlistfor default value

  23. 23

    MVC - DropDownListFor Enum - Set Default Value

  24. 24

    Set @Html.Dropdownlistfor to session in MVC 4

  25. 25

    Set @Html.Dropdownlistfor to session in MVC 4

  26. 26

    Setting default value to Html.DropDownListFor

  27. 27

    Wrong value binding with Html.DropDownListFor

  28. 28

    Get DropDownListFor selected value into Ajax.ActionLink call

  29. 29

    DropDownListFor<> not using my selected value if I change it in the model before call

HotTag

Archive