索引视图中的ASP.NET Core Model空错误 [英] ASP.NET Core Model null error in the Index view

查看:144
本文介绍了索引视图中的ASP.NET Core Model空错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的错误"部分.其他相关代码如下:

Please see the Error section below. Other related code is as follows:

模型:

public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }

BlogPostController

public IActionResult Index()
        {
            return View("~/Views/BlogsTest/Index.cshtml");
        }

查看 [Index.cshtml]:

View [Index.cshtml]:

@model IEnumerable<ASPCoreCodeFirstApp.Models.Blog>

<p>
    <a asp-controller="BlogPost" asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Url)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Url)
                </td>
                <td>
                    <a asp-controller="BlogPost" asp-action="Edit" asp-route-id="@item.BlogId">Edit</a>
                </td>
                <td>
                    <a asp-controller="BlogPost" asp-action="Create" asp-route-id="@item.BlogId">Edit</a>
                </td>
            </tr>
        }
    </tbody>
</table>

错误 @foreach (var item in Model)....line:

我可以看到Index方法被调用,当我将断点放在Index.cshtml文件的@foreach (var item in Model)行时,我得到Model为null,浏览器抛出以下错误. Blogs表中确实有数据.即使它没有数据,foreach循环也应该返回空结果,而不是NULL异常错误.由于确实调用了Index.cshtml视图,因此Index操作方法中的视图相对路径似乎已写入.我可能会缺少什么?:

I can see the Index method gets called and when I put the breakpoint at the @foreach (var item in Model) line of Index.cshtml file I get Model as null and the browser throws the following error. Blogs table does have data. Even if it did not have data the foreach loop should have returned empty result instead of an NULL exception error. The view relative path in the Index action method seems write since Index.cshtml view does get called. What I may be missing?:

NullReferenceException: Object reference not set to an instance of an object.
MoveNext in Index.cshtml, line 22

文件夹层次结构:

上面的模型在下面突出显示的Model.cs文件中:

Model above is in Model.cs file highlighted below:

推荐答案

您的视图是强类型视图,需要Blog对象的列表(准确地说是IEnumerable):

Your view is a strongly typed view requiring a list (to be precise, IEnumerable) of Blog objects :

@model IEnumerable<ASPCoreCodeFirstApp.Models.Blog>  

对于强类型视图,您必须传递一个视图类型,该对象需要从控制器操作中获取,视图引擎才能呈现该视图(即使它只是一个空列表):

For strongly typed views, you have to pass an object of the type that the view requires from your controller action for the view engine to be able to render the view ( Even if it's just an empty list ) :

View("ViewName", new List<Blog>())

这篇关于索引视图中的ASP.NET Core Model空错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆