【问题标题】:foreach in Razor Syntax in SubscribeEventMappingSubscribeEventMapping 中 Razor 语法中的 foreach
【发布时间】:2020-03-17 13:17:59
【问题描述】:

这是错误。 我正在尝试使用返回视图的 MVC 控制器从数据库中获取数据。

错误 CS1579 foreach 语句无法对“SubscriberEventMapping”类型的变量进行操作,因为“SubscriberEventMapping”不包含“GetEnumerator”的公共实例定义

@model WebApplicationLPISubscriber.Models.SubscriberEventMapping
@using WebApplicationLPISubscriber.Models;

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()



        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.SubscriberID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SubscriberID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.EventTypeName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Year)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CalendarStartDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CalendarEndDate)
                </th>



            </tr>
            </table>

            foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.SubscriberID)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.EventTypeName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Year)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.CalendarStartDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.CalendarEndDate)
                    </td>
                </tr> 
             }

【问题讨论】:

  • 可以分享controller中的Action吗?

标签: html asp.net-mvc razor html-helper


【解决方案1】:

错误发生在 foreach 中。您的模型声明 @model WebApplicationLPISubscriber.Models.SubscriberEventMapping 不是列表或集合类型。 Foreach 无法循环通过您的模型,因为它不是列表。

为了让 foreach 正常工作,请使用 List&lt;ModelName&gt;。请参阅下面的代码。

@using WebApplicationLPISubscriber.Models;
@model List<WebApplicationLPISubscriber.Models.SubscriberEventMapping>

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多