0

I can't find the solution for my problem, which is as follows: I'm creating an MVC online store in ASP.net. Everything was working fine until I tried putting items in my shopping cart. Then I get the "object reference not set to an instance of an object" error. I've tried several solutions who solved the issue for other users, but without any luck, unfortunately. Please let me know if you need more of My code:

namespace Oblig2.Models
{
    public class HandlevognViewModel
    {
        public List<Handlevogn> VognItems { get; set; }
        public decimal VognTotal { get; set; }
    }
}
@model Oblig2.Models.HandlevognViewModel

@{
    ViewBag.Title = "Handlevogn";
}

<h2>Handlevogn</h2>

<p class="button"> @Html.ActionLink("Til Kassen", "AdresseBetale", "Kassen")</p>

<table>
    <tr> 
        <th>Vare Navn</th>
        <th>Pris </th>
        <th>Antall </th>
    </tr>

    //This is where the error occurs.
    @foreach (var item in Model.VognItems)
    {
        <tr>
            <td>@item.Produkt.VareNavn</td>
            <td>@item.Produkt.Pris</td>
            <td>@item.Antall</td>
        </tr>
    }
</table>
namespace Oblig2.Controllers
{
    public class VarerController : Controller
    {
        private ButikkDBContext db = new ButikkDBContext();

        public ActionResult index()
        {
            return View(db.Produkt.ToList());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
        }
    }
}

    namespace Oblig2.Controllers
    {
        public class HandlevognController : Controller
        {
            ButikkDBContext db = new ButikkDBContext();

            public ActionResult index()
            {
                var vogn = Vogn.GetVogn(this.HttpContext);

                var viewModel = new HandlevognViewModel
                {
                    VognItems = vogn.GetVognItems(),
                    VognTotal = vogn.GetTotal()
                };

                return View(viewModel);
            }

            public ActionResult LeggTil(int id)
            {
                var nyVare = db.Produkt.Single(vare => vare.VareNr == id);
                var vogn = Vogn.GetVogn(this.HttpContext);
                vogn.LeggTilHandleVogn(nyVare);

                return RedirectToAction("index");
            }

            [ChildActionOnly]
            public ActionResult VognSammendrag()
            {
                var vogn = Vogn.GetVogn(this.HttpContext);
                ViewData["VognAntall"] = vogn.GetAntall();
                return PartialView("VognSammendrag");
            }

        }
   }

1 Answers1

1

in your model, add a private field:

private List<Handlevogn> vognitems = new List<Handlevogn>();
public List<Handlevogn> VognItems { get { return vognitems; } set { vognitems = value; }

VognItems is null, because you haven't instantiated it.

Jonesopolis
  • 23,589
  • 10
  • 63
  • 106
  • I added the fields you suggested to my HandleVognViewModel, but unfortunately it didn't solve the problem. I get the same error message. – user2947695 Nov 02 '13 at 13:24