c# - ASP.NET - 模型不存在

标签 c# html asp.net-mvc

我在 View 中使用 foreach 和 HTML 列表时遇到问题。 Controller 调用一个返回多个项目的 IEnumerable 的服务,我试图在我的 View 中将这些项目显示为 HTML 列表。

但是,我在 View 的第 7 行收到“CS0103:名称‘模型’在当前上下文中不存在”。据我所知,我已经掌握了显示模型内容的正确语法。

出了什么问题?

我有这样一个模型:

public class NewsItem
{
    public DateTime PublishDate { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Content { get; set; }
}

和这样的 Controller :

public ActionResult Index()
{
    NewsReader newsReader = new NewsReader();
    var newsItems = newsReader.GetNewsItems();

    return View(newsItems);
}

我的观点:

@model IEnumerable<Site.Services.News.NewsItem>
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@foreach(var item in model)
{
    <ul>
        <li>@Html.DisplayFor(m => model.Title)</li>
    </ul>
}

最佳答案

您应该在 DisplayFor 中将“model”大写并使用“item”:

@foreach(var item in Model)
{
    <ul>
        <li>@Html.DisplayFor(m => item.Title)</li>
    </ul>
}

关于c# - ASP.NET - 模型不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30569175/

相关文章:

php - 扩展浏览器宽度时的布局问题

c# - DotNetZip 从 MemoryStream 打开 zip 文件

c# - 有使用 InternalsVisibleToAttribute 的简单方法吗?

c# - .NET正则表达式识别 `if .. then .. else .. endif`

c# - 单元测试 C# VS15

c# - 在 Controller 中访问表单值,我试图将整个网格发布回发布方法 ASP.net MVC

html - css 内联 block 与 float

javascript - 结合D3可视化

asp.net - 尝试为 Medium Trust 进行开发是否会失败?

c# - 在 Asp.net MVC 中,是否建议 View 模型从域模型派生?