Selected value for dropdownlistfor in mvc4

Ali Shahzad

I have created a ViewBag selectlist in Edit action and set the value to be selected as:

ViewBag.Doors = new SelectList(
    new[]
    {
        new {ID = 1, Name="1-Door"},
        new {ID = 2, Name="2-Doors"},
        new {ID = 3, Name="3-Doors"},
        new {ID = 4, Name="4-Doors"},
        new {ID = 5, Name="5-Doors"},
        new {ID = 6, Name="6-Doors"},
        new {ID = 7, Name="7-Doors"}
    },
    "ID", "Name", advert.Doors);

But in the view the value for the dropdown is not selected by default.

My view code is:

@Html.DropDownListFor(model => model.Doors, (SelectList)ViewBag.Doors, "--Select--")
@Html.ValidationMessageFor(model => model.Doors)

The property Doors will be numeric 1,2,3,..

Imad Alazani

How will I do it using Annonymous type in ViewBag ?

Overload

enter image description here

Controller Action Method

public ActionResult DropDownListFor()
{
    ViewBag.Doors = new SelectList(
                        new[]
                        {
                            new {Value = 1,Text="1-Door"},
                            new {Value = 2,Text="2-Door"},
                            new {Value = 3,Text="4-Door"},
                            new {Value = 4,Text="4-Door"},
                            new {Value = 5,Text="5-Door"},
                            new {Value = 6,Text="6-Door"},
                            new {Value = 7,Text="7-Doors"}
                        }, "Value", "Text", 7);
    return View();
}

View

@Html.DropDownList("Doors")






How will I do it using SelectListItem?

Action Method

[HttpGet]
public ActionResult DropDownListFor()
{
    List<SelectListItem> items = new List<SelectListItem>();

    items.Add(new SelectListItem { Text = "Action", Value = "0" });
    items.Add(new SelectListItem { Text = "Drama", Value = "1" });
    items.Add(new SelectListItem { Text = "Comedy", Value = "2", 
                                                             Selected = true });
    items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" });
    ViewBag.MovieType = items;

    return View();
}

View

@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.DropDownList("MovieType")
}






How will I do it using View Model?

View

@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.DropDownListFor(m => m.Id, Model.DDLList, "Please select");
}

Action Method

[HttpGet]
public ActionResult DropDownListFor()
{
    return View(new Models.Dropdown());
}

View Model

public class Dropdown
{
    public string Id { get; set; }
    public List<SelectListItem> DDLList
    {
        get
        {
            return new List<SelectListItem>() 
            { 
                new SelectListItem
                { 
                    Text = "1-Door", 
                    Value = "1", 
                    Selected = true
                },
                new SelectListItem
                { 
                    Selected = false, 
                    Value = "2", 
                    Text = "2-Door"
                }
            };
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Html.DropdownListFor selected value not being set

From Dev

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

From Dev

DropDownListFor not always showing selected value

From Dev

Get DropDownListFor selected value into Ajax.ActionLink call

From Dev

dropdownlistfor selected value with viewdata

From Dev

MVC, DropDownListFor in partialview, onchange should store selected value _somewhere_

From Dev

Getting selected Index from DropDownListFor MVC

From Dev

MVC DropDownListFor - Selected Value ViewModel Class

From Dev

MVC update ListboxFor with selected values from DropDownListFor

From Dev

Kendo MVC DropDownListFor does not bind selected value to model

From Dev

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

From Dev

Using selected value in @Html.DropDownListFor

From Dev

DropDownListFor with an item value 0 always selected

From Dev

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

From Dev

DropDownListFor ASP MVC4

From Dev

selected value of radio button mvc4

From Dev

Unable to get @Html.DropDownListFor Selected Value

From Dev

MVC4 dropdownlist selected value

From Dev

Html.DropDownListFor gives error if no value selected

From Dev

Getting selected Index from DropDownListFor MVC

From Dev

'dropdownlistfor' controller not taking the selected value

From Dev

ASP.NET MVC @Html.DropdownListFor wrong selected value

From Dev

DropDownListFor selected value not working

From Dev

MVC update ListboxFor with selected values from DropDownListFor

From Dev

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

From Dev

MVC DropDownListFor selected value not being selected

From Dev

DropDownListFor not setting selected value with different names

From Dev

MVC DropdownlistFor set selected value not working?c#

From Dev

Html.DropDownListFor set selected value

Related Related

  1. 1

    Html.DropdownListFor selected value not being set

  2. 2

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

  3. 3

    DropDownListFor not always showing selected value

  4. 4

    Get DropDownListFor selected value into Ajax.ActionLink call

  5. 5

    dropdownlistfor selected value with viewdata

  6. 6

    MVC, DropDownListFor in partialview, onchange should store selected value _somewhere_

  7. 7

    Getting selected Index from DropDownListFor MVC

  8. 8

    MVC DropDownListFor - Selected Value ViewModel Class

  9. 9

    MVC update ListboxFor with selected values from DropDownListFor

  10. 10

    Kendo MVC DropDownListFor does not bind selected value to model

  11. 11

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

  12. 12

    Using selected value in @Html.DropDownListFor

  13. 13

    DropDownListFor with an item value 0 always selected

  14. 14

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

  15. 15

    DropDownListFor ASP MVC4

  16. 16

    selected value of radio button mvc4

  17. 17

    Unable to get @Html.DropDownListFor Selected Value

  18. 18

    MVC4 dropdownlist selected value

  19. 19

    Html.DropDownListFor gives error if no value selected

  20. 20

    Getting selected Index from DropDownListFor MVC

  21. 21

    'dropdownlistfor' controller not taking the selected value

  22. 22

    ASP.NET MVC @Html.DropdownListFor wrong selected value

  23. 23

    DropDownListFor selected value not working

  24. 24

    MVC update ListboxFor with selected values from DropDownListFor

  25. 25

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

  26. 26

    MVC DropDownListFor selected value not being selected

  27. 27

    DropDownListFor not setting selected value with different names

  28. 28

    MVC DropdownlistFor set selected value not working?c#

  29. 29

    Html.DropDownListFor set selected value

HotTag

Archive