1

Here I have a model class..

public class MyDrop
{
    public int id { get; set; }
    public string value { get; set; }
}

In my controller I use this code....

public ActionResult Index(){

    var GenreLst = new List<MyDrop>();

    MyDrop a = new MyDrop();
    a.id = 1;
    a.value = "hello";

    MyDrop b = new MyDrop();
    b.id = 2;
    b.value = "hello2";

    GenreLst.Add(a);
    GenreLst.Add(b);
    ViewBag.ShowDropDown = new SelectList(GenreLst); 


}

Then in my view I add this code...

@Html.DropDownList("ShowDropDown", "All")

After run the project.It show a dropdown list. But not showing correct values.

In the page source it likes this....

<select id="ShowDropDown " name="ShowDropDown "><option value="">All</option>
<option>DropDown.Models.MyDrop</option>
<option>DropDown.Models.MyDrop</option>
</select>

How do I solve this???

I was Expected like this.....

<select id="ShowDropDown " name="ShowDropDown "><option value="0">All</option>
<option value="1">hello</option>
<option value="2">hello2</option>
</select>

Please Help......

1
  • 3
    You need to specify the Value and Text properties of SelectList - ViewBag.ShowDropDown = new SelectList(GenreLst, "id", "value"); Note that the first option will be (and needs be) <option value="">All</option> (not value="0") Commented Mar 10, 2015 at 10:35

1 Answer 1

3
public ActionResult Index(){

    var GenreLst = new List<MyDrop>();

    MyDrop a = new MyDrop();
    a.id = 1;
    a.value = "hello";

    MyDrop b = new MyDrop();
    b.id = 2;
    b.value = "hello2";

    GenreLst.Add(a);
    GenreLst.Add(b);
    ViewBag.ShowDropDown = new SelectList(GenreLst, "id", "value"); 


}

In your View:

@{
SelectList list = ViewBag.ShowDropDown;
}

@Html.DropDownList("DROPDOWNID", list, "All")
Sign up to request clarification or add additional context in comments.

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.