Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 at 14-Mar-13 2:18am
   
what's your actual need? Load Dropdown?
anupamsingh130989 at 15-Mar-13 0:38am
   
yes ?
I want to load dropdown from my model. not from controller action
anupamsingh130989 at 14-Mar-13 6:24am
   
yes ?
I want to load dropdown from my model. not from controller action
anupamsingh130989 at 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 at 14-Mar-13 7:27am
   
still the problem is same.
Jameel Moideen at 15-Mar-13 1:43am
   
without controller and action how can u return a model to a view?
anupamsingh130989 at 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 _ at 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
0 OriginalGriff 565
1 Maciej Los 319
2 Richard MacCutchan 220
3 BillWoodruff 164
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Mar 2013
Copyright © CodeProject, 1999-2014
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