5

i am new in mvc. so i populate dropdown this way

public ActionResult New()
{
    var countryQuery = (from c in db.Customers
                        orderby c.Country ascending
                        select c.Country).Distinct();
    List<SelectListItem> countryList = new List<SelectListItem>();
    string defaultCountry = "USA";
    foreach(var item in countryQuery)
    {
        countryList.Add(new SelectListItem() {
                        Text = item, 
                        Value = item, 
                        Selected=(item == defaultCountry ? true : false) });
    }
    ViewBag.Country = countryList;
    ViewBag.Country = "UK";
    return View();       
}

@Html.DropDownList("Country", ViewBag.Countries as List<SelectListItem>)

i like to know how can i populate dropdown from model and also set default value. any sample code will be great help. thanks

4
  • @Html.DropDownList("Country", ViewBag.Country as List<SelectListItem>, "DefaultValueHere") Commented Apr 10, 2014 at 14:51
  • you should use Model instead of ViewBag. stackoverflow.com/questions/6807256/… Commented Apr 10, 2014 at 15:03
  • 1
    @DanielMelo: actually, that parameter determines the name that should be shown for the "empty" option, not a default value. Commented Apr 10, 2014 at 16:30
  • Judging by the @Html... snippet, ViewBag.Country = countryList; should read ViewBag.Countries = "UK"; Commented Jul 20, 2016 at 10:56

3 Answers 3

6

Well this is not a great way to do this.

Create a ViewModel that will hold everything you want to be rendered at the view.

public class MyViewModel{

  public List<SelectListItem> CountryList {get; set}
  public string Country {get; set}

  public MyViewModel(){
      CountryList = new List<SelectListItem>();
      Country = "USA"; //default values go here
}

Fill it with the data you need.

public ActionResult New()
{
    var countryQuery = (from c in db.Customers
                        orderby c.Country ascending
                        select c.Country).Distinct();
    MyViewModel myViewModel = new MyViewModel ();

    foreach(var item in countryQuery)
    {
        myViewModel.CountryList.Add(new SelectListItem() {
                        Text = item, 
                        Value = item
                        });
    }
    myViewModel.Country = "UK";



    //Pass it to the view using the `ActionResult`
    return ActionResult( myViewModel);
}

At the view, declare that this view is expecting a Model with type MyViewModel using the following line at the top of the file

@model namespace.MyViewModel 

And at anytime you may use the Model as you please

@Html.DropDownList("Country", Model.CountryList, Model.Country)
Sign up to request clarification or add additional context in comments.

1 Comment

what is this line Selected=(item == defaultCountry ? true : false) }); where is defaultCountry ?? it is not there in code.
3

You can't set the default value using Html.DropDownList, if you want to have a default value, the property itself should have a default value.

private string country;
public string Country
{
    get { return country ?? "UK"; }
    set { country = value; }
}

Then, when the drop down list renders, as long as "UK" is actually a value for one of the options, it will be automatically set to that.

Comments

0

If the DropDownList is filled in the controller and sent to the view via ViewBag, you can do:

ViewBag.MyName = new SelectList(DbContextname.Tablename, "Field_ID", "Description",idtobepresented); 

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.