c# - 获取错误 : Object reference not set to an instance of an object

标签 c# asp.net asp.net-mvc razor

<分区>

谁能帮我解决这个问题:

我需要在我的 _DeliveryDetailsPatial.cshtml 中显示动态数据。每次用户进入“投递信息”页面或“新地址”页面时都会调用此方法。每次我要导航到这些页面中的任何一个时,我都会不断收到此错误:

Object reference not set to an instannce of an object

我该如何解决这个问题?

谢谢!

这是我的代码:

_DeliveryDrtailsPartial:

@model QuiznosOnlineOrdering.Models.StoreViewModel
<table id="orderTable" class="table">
        <tr>
            <td class="t">Item</td>
            <td class="t">Quantity</td>
            <td class="t">Item Price</td>
        </tr>
        @foreach (var item in Model.StoreAddress)
        {
            <tr>
                <td>@Html.DisplayFor(model => item.NOM)</td>
                <td>@Html.DisplayFor(model => item.ADRESSE)</td>
                <td>@Html.DisplayFor(model => item.VILLE)</td>
            </tr>
        }
    </table>

StoreViewModel:

    public IEnumerable<Store> StoreAddress { get; set; }
    public IEnumerable<StoreHour> StoreHour { get; set; }
    public IEnumerable<ReferenceAcceptedPayment> StoreAcceptedPayment { get; set; }

DeliveryController:

[HttpGet]
public ActionResult GetStoreDetails()
{
    StoreViewModel store = new StoreViewModel();
    store.StoreAddress = from s in db.Store
                             select s;

    return View();
}

最佳答案

[HttpGet]
public ActionResult GetStoreDetails()
{
    StoreViewModel store = new StoreViewModel();
    store.StoreAddress = from s in db.Store
                             select s;

    return View(store);
}

你还没有通过模型

关于c# - 获取错误 : Object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27791996/

相关文章:

没有主要方法的 C# args?

c# - 检查指定类型是否匹配泛型类型?

asp.net配置读取小节错误

asp.net - GridView AutoGenerateColumns 和排序

asp.net - 将用户添加到角色 ASP.NET 标识

asp.net-mvc - 当用户没有互联网时,是否可以从缓存中设置自定义错误页面

asp.net-mvc - 如何在 ASP.NET MVC 4 中忽略特定 View 文件夹或特定 cshtml 文件的路由

c# - 在调用 c++ 代码时出现访问冲突错误是 c#

c# - 使用 SimpleMembership/Sql Server CE、MongoDB 管理用户配置文件

c# - Task.Factory.StartNew() 是否保证至少创建一个新线程?