Getting error: Object reference not set to an instance of an object

Getting error: Object reference not set to an instance of an object

谁能帮我解决这个问题:

我需要在 _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);
}

因此,当您尝试读取模型的属性时,您会遇到异常,因为模型为空。

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

    return View(store);
}

你还没有通过模型

你需要像这样传递模型

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

    return View(store);
}

以下链接也将帮助您将视图传递给局部视图。

 return PartialView("_ViewPage",model);

Setting models in asp.net mvc views and partial views