1

Can anyone help me with this:

I need to display dynamic data in my _DeliveryDetailsPatial.cshtml. This is called every time the user is in the Delivery Information page or New Address page. Every time I am to navigate to either of these pages i keep getting this ERROR:

Object reference not set to an instannce of an object

How can I solve this?

Thank you!

Here's my code:

_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();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aianLee
  • 97
  • 1
  • 2
  • 11
  • post the full stack trace please but offhand guessing it has to do with the fact that Model.StoreAddress is null – Bill Dinger Jan 06 '15 at 04:15

3 Answers3

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

    return View(store);
}

You haven't passed model

Mahesh Malpani
  • 1,782
  • 16
  • 27
  • thanks the data i need from the database are now being displayed. But I get another error, when I click a button (HttpPost), I still get the same Error. Thanks! – aianLee Jan 06 '15 at 08:19
2

You are never actually injecting the model into the view.

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

    return View(store);
}

As a result, when you try to read properties on your model, you get an exception since the model is null.

David L
  • 32,885
  • 8
  • 62
  • 93
0

You need to pass the model like this

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

    return View(store);
}

Also following are the links which will help you also in passing views to partial view also.

 return PartialView("_ViewPage",model);

Setting models in asp.net mvc views and partial views

Shan Khan
  • 9,667
  • 17
  • 61
  • 111