Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I want to use below property in my view with dropdownbox.
How could I ?
I have tried so many ways but :(
Please help


CSS
public IEnumerable<SelectListItem> Topic
{
    get
    {
        return  new[]
    {
        new SelectListItem { Value = "1", Text = "Science", Selected=true },
        new SelectListItem { Value = "2", Text = "History" },
        new SelectListItem { Value = "3", Text = "Physics" },
    };
    }
}
Posted
Comments
Jameel VM 14-Mar-13 2:18am    
what's your actual need? Load Dropdown?
[no name] 15-Mar-13 0:38am    
yes ?
I want to load dropdown from my model. not from controller action
[no name] 14-Mar-13 6:24am    
yes ?
I want to load dropdown from my model. not from controller action
[no name] 14-Mar-13 7:27am    
have tried


public SelectList Topic
{
get
{
return new SelectList(
new List<SelectListItem> {
new SelectListItem { Value = "1", Text = "Science", Selected = true },
new SelectListItem { Value = "2", Text = "History" },
new SelectListItem { Value = "3", Text = "Physics" }
}
);
}
}
[no name] 14-Mar-13 7:27am    
still the problem is same.

1 solution

try this
C#
public static class DropDownList<t>
   {
       public static SelectList LoadItems(IList<t> collection, string value, string text)
       {
           return new SelectList(collection, value, text);
       }
   }
Call the method from the controller like Below.
ViewData["Executives"] =
                DropDownList<executives>.LoadItems(
                    objExecutivesDbContext.Executives.ToList(), "ExecutiveId", "ExecutiveName ");
Call the viewData from the View like below
<div class="editor-field">
            @Html.DropDownListFor(model => model.ExecutiveId, (IEnumerable<selectlistitem>) ViewData["Executives"], "--Select--")

Hope this helps
 
Share this answer
 
Comments
[no name] 30-Mar-13 6:33am    
thanx . i resolved it myself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900