MVC - DropDownListFor Enum - Set Default Value

Anup

My Dropdown works properly with following code :-

@Html.DropDownListFor(model => model.StopMonth, Enum.GetNames(typeof(Models.InputMonths)).Select(e => new SelectListItem { Text = e }), new { @class = "form-control"})

public enum InputMonths
    {
        January,
        February,
        March,
        April,
        May,
        June,
        July,
        August,
        September,
        October,
        November,
        December
    }

But when it is displayed in View, I need DropDown to display ---Select Month--- as default value. So that i can check required validation in jQuery Script.

How to do this?

Mohsen Esmailpour

First add value to your enum

public enum InputMonths {
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    .
    .
    .        
}

Then decorate your Model with:

[Required]
[Range(1, 12, ErrorMessage = "Select a month"))]
public StopMonth StopMonth { get; set; }

And finally:

@Html.DropDownListFor(
    model => model.StopMonth,
    Enum.GetNames(typeof(Models.InputMonths))
        .Select(e => new SelectListItem { Text = e }),
    "-- Select a month --",
    new { @class = "form-control"})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set Default value in MVC 4 razor DropDownListFor

From Dev

How to set state of dropdownlistfor default value

From Dev

@Html.DropDownListFor how to set default value

From Dev

How to select default value for DropDownListFor using ASP.NET MVC

From Dev

How to set default selected value for Html.DropDownListFor

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

How to set the default value of enum type variable?

From Dev

how to set Enum in DropDownListFor and keep selectet item when back to this page in mvc?

From Dev

DropDownlistFor with a null-value as default

From Dev

MVC 5 RadioButtonFor enum, the default enum value zero is always selected?

From Dev

Set the default value in MVC web application

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

Why is my default value for DropDownListFor not working properly?

From Dev

Setting default value to Html.DropDownListFor

From Dev

Populate DropDownListFor and default value is guid parameter

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

From Dev

Want to set default value selected in JComboBox populated by enum

From Dev

MVC DropDownListFor - Selected Value ViewModel Class

From Dev

MVC DropDownListFor with ViewBag not passing value to controller

From Dev

Selected value for dropdownlistfor in mvc4

From Dev

How to get the value of a DropDownListFor in MVC6

From Dev

MVC DropDownListFor selected value not being selected

From Java

Html.DropdownListFor selected value not being set

From Dev

Html.DropDownListFor set selected value

From Dev

Protobuf: enum with default value

From Dev

Protobuf: enum with default value

From Dev

Current month in DropDownListFor by default in Asp.Net mvc

Related Related

  1. 1

    How to set Default value in MVC 4 razor DropDownListFor

  2. 2

    How to set state of dropdownlistfor default value

  3. 3

    @Html.DropDownListFor how to set default value

  4. 4

    How to select default value for DropDownListFor using ASP.NET MVC

  5. 5

    How to set default selected value for Html.DropDownListFor

  6. 6

    MVC DropdownlistFor set selected value not working?c#

  7. 7

    How to set the default value of enum type variable?

  8. 8

    how to set Enum in DropDownListFor and keep selectet item when back to this page in mvc?

  9. 9

    DropDownlistFor with a null-value as default

  10. 10

    MVC 5 RadioButtonFor enum, the default enum value zero is always selected?

  11. 11

    Set the default value in MVC web application

  12. 12

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

  13. 13

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

  14. 14

    Why is my default value for DropDownListFor not working properly?

  15. 15

    Setting default value to Html.DropDownListFor

  16. 16

    Populate DropDownListFor and default value is guid parameter

  17. 17

    Set @Html.Dropdownlistfor to session in MVC 4

  18. 18

    Set @Html.Dropdownlistfor to session in MVC 4

  19. 19

    Want to set default value selected in JComboBox populated by enum

  20. 20

    MVC DropDownListFor - Selected Value ViewModel Class

  21. 21

    MVC DropDownListFor with ViewBag not passing value to controller

  22. 22

    Selected value for dropdownlistfor in mvc4

  23. 23

    How to get the value of a DropDownListFor in MVC6

  24. 24

    MVC DropDownListFor selected value not being selected

  25. 25

    Html.DropdownListFor selected value not being set

  26. 26

    Html.DropDownListFor set selected value

  27. 27

    Protobuf: enum with default value

  28. 28

    Protobuf: enum with default value

  29. 29

    Current month in DropDownListFor by default in Asp.Net mvc

HotTag

Archive