0

我要么得到“你需要一个 ; 这里”或“最佳重载匹配有无效参数”错误。

        <tbody>
            <tr>
            @for (int i = 0; i < startDay; ++i)
            {
                @:<td><span></span><span></span></td>
            }
            @for (int j = startDay; j < ((numberOfDays + startDay) - 1); ++j)
            {
                <td>
                    <span>@startCount</span>
                    <br />
                    <span>
                        @{
                            var todaysEvents = Model.ToList().FindAll(d => d.CalDate.Day == j);
                            foreach(HTMLMVCCalendar.Models.CalendarModel eventsToday in todaysEvents)
                            {
                                foreach(HTMLMVCCalendar.Models.EventModel eventToday in eventsToday.CalEvents)
                                {
                                    @eventToday.DayCode.ToString // error here
                                    @:<br />
                                    @eventToday.Subject // error here
                                    @:<br />
                                    @eventToday.EventDesc //error here
                                } 
                                @:<br /> 
                            }
                        }
                    </span>
                </td>
                if ((j + 1) % 7 == 0)
                {
                    @:</tr><tr>
                }
                @++startCount;
            }
            </tr>
        </tbody>
4

1 回答 1

4

使用<text>元素而不是@在要显示的每条文本之前使用。兑换:

foreach(HTMLMVCCalendar.Models.EventModel eventToday in eventsToday.CalEvents)
{
    @eventToday.DayCode.ToString // error here
    @:<br />
    @eventToday.Subject // error here
    @:<br />
    @eventToday.EventDesc //error here
} 

至:

foreach(HTMLMVCCalendar.Models.EventModel eventToday in eventsToday.CalEvents)
{
    <text>
        @eventToday.DayCode:<br />
        @eventToday.Subject:<br />
        @eventToday.EventDesc
    </text>
} 

我还删除了.ToString(缺少括号),因为在使用@符号时对象被隐式转换为字符串。

另一件事,你有这个代码:

@{
    var todaysEvents = Model.ToList().FindAll(d => d.CalDate.Day == j);
    foreach(HTMLMVCCalendar.Models.EventModel eventToday in eventsToday.CalEvents) 
    {
        ...
    }
}

这不必要地增加了你的嵌套。与我们的直觉相反,以 开头的 Razor 块@实际上并没有像在 C# 中那样形成一个封闭的范围。因此,您可以将其写为:

@{
    var todaysEvents = Model.ToList().FindAll(d => d.CalDate.Day == j);
}
@foreach(HTMLMVCCalendar.Models.EventModel eventToday in todaysEvents.CalEvents) 
{
    ...
}

如您所见(如果您运行它),todaysEvents仍然可以访问。

于 2012-04-11T17:25:23.057 回答