Search code examples
razordropdownboxdropdownlistfor

Razor DropDownListFor without passing model


How to display this list

var dates = new List<SelectListItem>
{
    new SelectListItem {Selected = false, Text = "Show offers for all dates", Value = ""},
    new SelectListItem {Selected = false, Text = "Show offers for today", Value = today.ToShortDateString()},
    new SelectListItem {Selected = false, Text = "Show offers for this week", Value = endOfThisWeek.ToShortDateString()},
    new SelectListItem {Selected = false, Text = "Show offers for this month", Value = endOfThisMonth.ToShortDateString()},
    new SelectListItem {Selected = false, Text = "Show offers for further out", Value = all.ToShortDateString()},
};

in

    @Html.DropDownListFor()

?

As far as I can see the DropDownListFor requires a model as a first argument but I don't require a model for the dropdown to work for my purposes.

Thanks


Solution

  • You can use

    @Html.DropDownList("name", dates)
    

    The name is the name of your dropdown (used in the id and name of the dropdown).

    The @Html.DropDownListFor() is indeed for the cases when you have a model.