Skip to content

Instantly share code, notes, and snippets.

@venblee
Last active December 18, 2015 21:38
Show Gist options
  • Save venblee/5848506 to your computer and use it in GitHub Desktop.
Save venblee/5848506 to your computer and use it in GitHub Desktop.
Select2 - > SelectList with viewmodel , DropDown with preload values.
List<SelectListItem> country = new List<SelectListItem>();
country.Add(new SelectListItem{Text = "India", Value="India"});
// ViewModel
public class test
{
public IEnumerable<SelectListItem> thelist { get; set; }
public int theitemid { get; set; }
}
public class car
{
public int Id { get; set; }
public string name { get; set; }
}
// The Builder
public test Builder()
{
List<car> P = new List<car>();
P.Add(new car {Id = 1, name = "ford"});
P.Add(new car {Id = 2, name = "Bmw"});
P.Add(new car {Id = 3, name = "VW"});
var result = new test()
{
theitemid = 3,
thelist =
P.Select(list => new SelectListItem()
{
Text = list.name,
Value = list.Id.ToString()
})
};
return result;
}
// The View
@Html.DropDownListFor(m => m.theitemit, Model.theList , new{id="04"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment