Reputation: 51
I have a problem displaying the view and I have such a message: When starting the https://localhost:44370/Product/Index view should receive a list and receive messages as above where to look for an error
Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
Opis: Podczas wykonywania bieżącego żądania sieci Web wystąpił nieobsługiwany wyjątek. Aby uzyskać dodatkowe informacje o błędzie i miejscu jego występowania w kodzie, przejrzyj ślad stosu.
Szczegóły wyjątku: System.NullReferenceException: Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
Błąd źródła:
Wiersz 24: </tr>
Wiersz 25:
Wiersz 26: @foreach (var item in Model) - it is marked in red {
Wiersz 27: <tr>
Wiersz 28: <td>
Plik źródłowy: E:\Visual\Shop\Shop\Views\Product\Index.cshtml Wiersz: 26
When starting the https: // localhost: 44370 / Product / Index view should receive a list and receive messages as above where to look for an error
This my View:
@model IEnumerable<Shop.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</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>
}
</table>
This is controller:
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Shop.Controllers
{
public class ProductController : Controller
{
static List<Product> _products = new List<Product>
{
new Product {Id = 1, Name = "Myszka", Description = "Opis myszki", Price = 500},
new Product {Id = 2, Name = "Klawiatura", Description = "Opis klawiatury", Price = 600},
new Product {Id = 3, Name = "Słuchawki", Description = "Opis słuchawki", Price = 1500},
new Product {Id = 4, Name = "Monitor", Description = "Opis myszki", Price = 2500}
};
// GET: Product
public ActionResult Index()
{
var model = _products;
return View();
}
// GET: Product/Details/5
public ActionResult Details(int id)
{
var model = _products.FirstOrDefault(p => p.Id == id);
if (model == null)
return RedirectToAction("Index");
return View();
}
// GET: Product/Create
public ActionResult Create()
{
return View();
}
// POST: Product/Create
[HttpPost]
public ActionResult Create(Product collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Product/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Product/Edit/5
[HttpPost]
public ActionResult Edit(int id, Product collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Product/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Product/Delete/5
[HttpPost]
public ActionResult Delete(int id, Product collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Upvotes: 0
Views: 88
Reputation: 803
You need to pass the model to the view. When you don't pass the model into the view from the controller, the Model
in the view is set to null
. Then, when you want to execute the foreach
, basically you are trying to invoke a method on a null object, hence the exception
public ActionResult Index()
{
var model = _products;
return View(model);
}
Upvotes: 1