So I'm new to C# and i've been stuck on this for hours. I tried multiple passings (ViewBag, ViewData and also as a parameter). In my View it says it gets a NullPointerException at the foreach loop.
What I have tried:
this is my Model:
namespace SoPro3.Models
{
public class BookingModel
{
public int ladestand { get; set; }
public int fahrstrecke { get; set; }
public DateTime startzeit { get; set; }
public DateTime endzeit { get; set; }
}
}
this is my Controller:
namespace SoPro3.Controllers
{
public class BookingController : Controller
{
public ActionResult Index()
{
List<BookingModel> bookings = new List<BookingModel>() {
new BookingModel
{
ladestand = 10,
fahrstrecke = 100,
startzeit = new DateTime(2020, 10, 2, 8, 45, 0),
endzeit = new DateTime(2020, 10, 2, 12, 45, 0)
},
new BookingModel
{
ladestand = 65,
fahrstrecke = 340,
startzeit = new DateTime(2020, 05, 3, 9, 45, 0),
endzeit = new DateTime(2020, 05, 3, 11, 45, 0)
},
new BookingModel
{
ladestand = 30,
fahrstrecke = 150,
startzeit = new DateTime(2020, 09, 15, 22, 45, 0),
endzeit = new DateTime(2020, 09, 16, 0, 00, 0)
}
};
return View(bookings);
}
}
}
This is my View:
@model List<SoPro3.Models.BookingModel>
@{
ViewBag.Title = "Index";
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bookings</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<h1 class="display-4">Bookings</h1>
<div class="text-center">
<table>
<tr>
<th>Ladestand</th>
<th>Fahrstrecke</th>
<th>Startzeit</th>
<th>Endzeit</th>
</tr>
@foreach (var item in Model)
{
<tr>
<th>@item.ladestand</th>
<th>@item.fahrstrecke</th>
<th>@item.startzeit</th>
<th>@item.endzeit</th>
</tr>
}
</table>
}
</div>
</body>
</html>
I tried a few things like changing the model to IEnumeration<t> but it seems not to work aswell.