0

I've a view that displays list structure for the documents uploaded. However, this needs to change to create dropdownlist instead of list. below code creates list

@foreach (var item in Model) 
{
    <tr>
    <td>@Html.DisplayFor(modelItem => item.FileId)</td> 
    <td>@Html.DisplayFor(modelItem => item.FileName)</td>
    <td>@Html.ActionLink("Download", "Download", new { id=item.FileId})</td>
    </tr>
}

This needs to be changed to create a dropdownlist, how can I do that?

2
  • What exactly do you want to display in the dropdown list? FileId? FileName? Download link? Commented Oct 26, 2017 at 10:36
  • It's only Filename that would be displayed Commented Oct 26, 2017 at 10:38

3 Answers 3

1

You can also use with this

In controller

Public Actionresult HomeController()
{
    // your code goes here
    //create list of content you want to display on dropdown list
    //you can pass it on 2 ways 1. with model pass and 2.is with viewbag 
     Viewbag.countryList = listofCountry;

}

In View

Method1 with Model

<select>
@foreach(var item in Model)
{
<option value="@item.FileId">@ietm.FileName</option>
}
</select>

Method 2.

<select>
   @foreach(var item in Viewbag.countryList)
   {
          <option value="@item.FileId">@ietm.FileName</option>
   }
   </select>
Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple ways to render a dropdown list from a collection.

Here is how you do it with DropDownList helper

@Html.DropDownList("MySelect",new SelectList(Model,"FileId","FileName"))

This will render a SELECT element with name and Id attribute values set to "MySelect" and the options text will be the FileName and value will be the FileId.

Comments

0

You can use this sample.

@Html.DropDownListFor(model => model.FileId, model.FileItems.Select(x => new SelectListItem { Text = x.FileName, Value = x.FileId })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.