简体   繁体   English

C#ASP.NET MVC视图下拉编译错误CS0161

[英]C# ASP.NET MVC view drop down compilation error CS0161

@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?)" “编译器错误消息:CS1061:'IEnumerable'不包含'DayOfWeek'的定义,并且找不到扩展方法'DayOfWeek'接受类型为'IEnumerable'的第一个参数(您是否缺少using指令或程序集引用? )”

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. 它用于selectedItem变量。 Check this blog post: https://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx . 检查此博客文章: 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. 您必须使用元素列表和变量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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM