Friday 25 October 2013

Inline SelectList for MVC DropDownList

Below are examples of how to write predefined SelectList items of a DropDownList inline in ASP.NET MVC view.
@Html.DropDownListFor(model => model.IsExist,
         new SelectList(new List<Tuple<bool, string>>
         {
           new Tuple<bool, string>(false, "No"),                    
           new Tuple<bool, string>(true, "Yes"),                    
         }, "Item1", "Item2"))
@Html.DropDownListFor(model => model.IsExist,
         new SelectList(new []
         {
           new {ID=false, Name="No"},
           new {ID=true, Name="Yes"},
         }, "ID", "Name"))

No comments: