Last active
December 18, 2015 21:38
-
-
Save venblee/5848506 to your computer and use it in GitHub Desktop.
Select2 - > SelectList with viewmodel , DropDown with preload values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<SelectListItem> country = new List<SelectListItem>(); | |
country.Add(new SelectListItem{Text = "India", Value="India"}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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