Click here to Skip to main content
12,073,325 members (72,869 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# .NET C#4.0 .NET4 MVC3
Hi
I want to use below property in my view with dropdownbox.
How could I ?
I have tried so many ways but Frown | :(
Please help


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 13-Mar-13 21:08pm
Comments
Jameel Moideen 14-Mar-13 2:18am
   
what's your actual need? Load Dropdown?
anupamsingh130989 15-Mar-13 0:38am
   
yes ?
I want to load dropdown from my model. not from controller action
anupamsingh130989 14-Mar-13 6:24am
   
yes ?
I want to load dropdown from my model. not from controller action
anupamsingh130989 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" }
}
);
}
}
anupamsingh130989 14-Mar-13 7:27am
   
still the problem is same.
Jameel Moideen 15-Mar-13 1:43am
   
without controller and action how can u return a model to a view?
anupamsingh130989 15-Mar-13 1:49am
   
Thanx for reply Jameel

Actually I have defined Topic property Like
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" }
}
);
}
}
in my model I am having get accessor so I am binding data from model only.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try this
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
  Permalink  
Comments
Anupam Singh _ 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 15 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100