@model IEnumerable<Calendar.Models.CheckDays
<p>
@using (Html.BeginForm())
{
<table>
<tr>
<th>
@Html.ActionLink("Create New", "Create")
</th>
<th>
@Html.DropDownListFor(model => model.DayOfWeek, htmlAttributes: new { @class = "form-control" })
</th>
<th>
<input type="button" value="Search" />
</th>
</tr>
</table>
}
</p>
<tr>
<th>
@Html.DisplayNameFor(model => model.DayOfWeek)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DayOfWeek)
</td>
</td>
</tr>
}
I'm trying to create a drop down list so I can filter the the index results but I keep getting an error
"Compiler Error Message: CS1061: 'IEnumerable' does not contain a definition for 'DayOfWeek' and no extension method 'DayOfWeek' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)"
The line that errors out is
Html.DropDownListFor(model => model.DayOfWeek, htmlAttributes: new { @class = "form-control" })".
Do I need to do something in the model or do I have syntax errors?
You misunderstood what first parameter is for. It is to point where selected item should go for. It is for selectedItem variable. Check this blog post: https://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx . You have to create separate class for model with list of elements and variable SelectedDayOfWeek.
Model class:
public class CheckDaysViewModel
{
public IEnumerable<CheckDays> CheckDays {get;set;}
public IEnumerable<SelectListItem> CheckDaysAsSelectedList => this.CheckDays.Select(e => new SelectListItem(e.DayOfWeek, e.DayOfWeek));
public CheckDays SelectedDay {get;set;}
}
cshtml
@model CheckDaysViewModel
<p>
@using (Html.BeginForm())
{
<table>
<tr>
<th>
@Html.ActionLink("Create New", "Create")
</th>
<th>
@Html.DropDownListFor(m => m.SelectedDay, Model.CheckDaysAsSelectedList, null, htmlAttributes: new { @class = "form-control" })
</th>
<th>
<input type="submit" value="Search" />
</th>
</tr>
</table>
}
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(m => m.CheckDays.First().DayOfWeek)
</th>
</tr>
@foreach (var item in Model.CheckDays)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DayOfWeek)
</td>
</tr>
}
</table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.