Samantha J T Star
Samantha J T Star

Reputation: 32778

DropDownList or DropDownListFor in MVC3 Razor?

I have a report that shows a list with the following columns:

A key column
A dropdown list showing a list of possible status
A title

This is the razor code:

@foreach (AdminSummary adminSummary in @Model.AdminSummaries) {
                index++;
                <div class="rep_tr0">
                    <div class="rep_td0" id="rk_@(index)">@adminSummary.RowKey</div>
                    <div class="rep_td0"><a href="/Administration/Products/Edit?&[email protected]&[email protected]&[email protected]">Edit</a></div>            
                    <div class="rep_td0"><a href="/Administration/Products/Delete?&[email protected]&[email protected]&[email protected]">Delete</a></div>  
                    <div class="rep_td0">@Html.DropDownListFor(??, ??)</div>
                    </div>
            }

I also have the following class:

public static class AdminStatusReference
    {

        public static IEnumerable<SelectListItem> GetAdminStatusOptions()
        {
            return new[]
                {
                    new SelectListItem { Value = "1", Text = "Released"  },
                    new SelectListItem { Value = "2", Text = "Review" },
                    new SelectListItem { Value = "3", Text = "New" }
                };
        }

}

What I am not sure is how I can link these up. I know in the model I can store the list of status options something like this:

vm.StatusList  = GetAdminStatusOptions();

But how can I create the dropdownlist. I am very confused with all of the options of DropDownList and DropDownListFor. Which should I be using and how can I send in the list of statuses?

Upvotes: 0

Views: 822

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

What I am not sure is how I can link these up.

This will depend on how the model looks like.

But here's what you could do:

@for (var index = 0; index < Model.AdminSummaries.Count; index++)
{
    <div class="rep_tr0">
        <div class="rep_td0" id="rk_@(index)">
            @Model.AdminSummaries[index].RowKey
        </div>
        <div class="rep_td0">
            @Html.DropDownListFor(
                x => x.AdminSummaries[index].Status,
                AdminStatusReference.GetAdminStatusOptions()
            )
        </div>
        <div class="rep_td0">
            @Model.AdminSummaries[index].Title
        </div>
    </div>
}

Upvotes: 1

Related Questions