foreach(模型中的var项),未将对象引用设置为对象的实例

问题描述 投票:0回答:1

我使用Controller中的“List View”生成storeList.cshtml文件,在这个视图文件中,有一行:

foreach (var item in Model) {}

当我运行应用程序时,此行不断收到“Nullreference”错误,并且“对象引用未设置为对象的实例”错误。

我很困惑,因为:

1)该视图文件是自动生成的;那为什么不正确呢?

2)我使用了完全相同的方式(“列表视图”)并生成了许多其他视图文件,从未遇到过这个问题。

<table class="uk-table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.CategoryID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.typeID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.storeUrl)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.storedes)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.tags)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.image)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.metaTitle)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.metaDescription)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.popular)
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.CategoryID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.typeID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.storeUrl)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.storedes)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.tags)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.image)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.metaTitle)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.metaDescription)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.popular)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
        @Html.ActionLink("Details", "Details", new { id=item.id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.id })
    </td>
</tr>

}

调节器

 public ActionResult storeList()
    {
        return View();
    }

模型

 public partial class store
{
    public store()
    {
        this.Reviews = new HashSet<Review>();
    }

    public int id { get; set; }
    public Nullable<int> CategoryID { get; set; }
    public Nullable<int> typeID { get; set; }
    public string name { get; set; }
    public string storeUrl { get; set; }
    public string storedes { get; set; }
    public string tags { get; set; }
    public string image { get; set; }
    public string metaTitle { get; set; }
    public string metaDescription { get; set; }
    public Nullable<bool> popular { get; set; }

    public virtual Category Category { get; set; }
    public virtual ICollection<Review> Reviews { get; set; }
    public virtual Type Type { get; set; }
}

错误

你调用的对象是空的。

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

让我知道如何解决这个问题?

谢谢

.net asp.net-mvc-5
1个回答
1
投票

你可以随意改变它。

在模型中

public partial class store
{
    public int id { get; set; }
    public Nullable<int> CategoryID { get; set; }
    public Nullable<int> typeID { get; set; }
    public string name { get; set; }
    public List<store> MyStoreList {get;set;}
}

在控制器中

public ActionResult storeList()
{     
    store obj=new store();
    obj.MyStoreList = new List<store>();// Load your list using uery  
    return View(obj);
}

在cshtml中

@model projectname.Models.store

@foreach (var item in Model.MyStoreList) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.CategoryID )
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.typeID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.name)
    </td>       
</tr>
© www.soinside.com 2019 - 2024. All rights reserved.