1

Based on this code (auto-generated Control/View code from Visual Studio):

<div class="form-group">
    @Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @*Comment: Want to replace EditorFor to DropDownList("Banana, "Apple", "Orange"*@
        @Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })*@
        @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
    </div>
</div>

I would like to add something similar like:

@Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })

(also auto-generated code, but for UserId) instead of @html.EditorFor...

I was able to see list of UserName(value = Id) from db via Controller:

ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", Test.UserId);

Now, I don't want that ViewBag read from database, instead I want it to list 3 different strings that I will use to let user to chose for indata(meaning, limit them to choice one of these 3 string instead writing it).

Strings I want it to list:

  • "Banana"
  • "Apple"
  • "Orange"

How do I do that?

5 Answers 5

2

Try this,

@Html.DropDownList("DropDown", new List<SelectListItem>
                                 { new SelectListItem { Text = "Banana", Value = "1", Selected=true},
                                   new SelectListItem { Text = "Apple", Value = "2"},
                                   new SelectListItem { Text = "Orange", Value = "3"}
                                   }, "Select Fruit")

OR

Get value in model

@Html.DropDownListFor(x => x.Id, new List<SelectListItem>
                                 { new SelectListItem { Text = "Banana", Value = "1", Selected=true},
                                   new SelectListItem { Text = "Apple", Value = "2"},
                                   new SelectListItem { Text = "Orange", Value = "3"}
                                   }, "Select Fruit")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works well! Just a question, is this still a proper way in work area(e.g. average safety) or should I consider other solution?
@Nyprez Yes, Both are proper way. If you want selected value in your model than use second option otherwise first.
0

Simply change what you are assigning to ViewBag.UserId f.e. like this:

var fruits = new List<string> { "Banana", "Apple", "Orange" };
ViewBag.Fruits = fruits.Select(f => new SelectListItem { Text = f, Value = f });

2 Comments

Using @Html.DropDownList("Fruits", null, htmlAttributes: new { @class = "form-control" }) in View gave me the error: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Fruits'.. What am I doing wrong?
As a second parameter you have to pass the ViewBag.Fruits like this: @Html.DropDownList("Fruits", ViewBag.Fruits, htmlAttributes: new { @class = "form-control" })
0

This is how you can build your list items:

ViewBag.Fruits = new SelectList(
    new List<SelectListItem>
    {
        new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
        new SelectListItem { Selected = false, Text = "Banana", Value = 0},
        new SelectListItem { Selected = false, Text = "Apple", Value = 1},
    }, "Value" , "Text", 1);

1 Comment

Tried to call it in view by: @Html.DropDownList("Fruits", null, htmlAttributes: new { @class = "form-control" }) but I get the error: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Fruits'.
0

Here is another way of doing it using enum

public enum Fruit { Banana, Apple, Orange }

@Html.DropDownList("FruitSelection", 
                    new SelectList(Enum.GetValues(typeof(Fruit))),
                    "Select Fruit",
                    new { @class = "form-control" })

1 Comment

Do I add public enum Fruit in controller? If I do so, I get the error "Cant resolve symbol 'Fruit'" and for the other 3 as well.
0

MVC Controler

  ViewBag.PageSort = new List <SelectListItem> ()
         {
             new SelectListItem () {Value = "1", Text = "Discounts"},
             new SelectListItem () {Value = "2", Text = "New Items"},
             new SelectListItem () {Value = "3", Text = "Lowest Price"},
             new SelectListItem () {Value = "4", Text = "Highest Price"}
         };

in View

@Html.DropDownList("PageSort", ViewBag.PageSortas SelectList, "- Select Sort -", new { @id = "SortID", @name = "SortID", @class = "form-control" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.