Pomster
Pomster

Reputation: 15197

Create a list to display the contents of a dictionary?

I have a dictionary that i would like to display as a list on my view page. At the moment i can create a list but i cant set a dynamic size or populate it with my dictionary. I would like to do something like a foreach, or if there is a faster just to list function?

Im new to MVC sorry for poor code.

@model Dictionary<string, string>   
@{
    ViewBag.Title = "Download";
}   
<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2> 
    <br>  
    @{
        var filelist = @Model.ToList();
    }    
    <Div>
     <select name="Fileslist" size="5">
      <option>Count</option>
      <option>text2</option>
      <option>text3</option>
      <option>text4</option>
      <option>text5</option>
     </select>
    </Div>   
</hgroup>

Upvotes: 2

Views: 327

Answers (2)

oleksii
oleksii

Reputation: 35905

You can use @Html.DropDownListFor which supports Dictionary directly.

Take a look at this answer for more details.

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

A simple Dictionary to List would be

Dictionary<string, string> myDictionary = new Dictionary<string, string>();

// Add some values in dictionary

var res = myDictionary.Select(x => x.Key + "-" + x.Value).ToList()

Upvotes: 2

Related Questions