I am struggeling with data binding and Razor pages
I was not able to find a solution. Propably I am asking not the right questions. So I hope you can help me out to point me to the right direction.
I want a page to create bookings to collect all necessary data to post it to a web api when everything is done.
On that page I want to collect necessary data for the wooking and want to add x Booking Details to the Booking.
Datamodel
public class Booking
{
[Key]
public int Id { get; set; }
public string Reference { get; set; }
public List<BookingDetail> BookingDetails { get; set; }
}
public class BookingDetail
{
[Key]
public int Id { get; set; }
public int Pieces { get; set; }
public string Marks { get; set; }
}
What I would like to to is to create a create page for a booking. So far I followed this instructions, which is working Link
This missing part is that I would like to add new Booking Details as well on the same Razor page.
So I want to see a list of all added booking Details and I want to have textboxes to add new booking Details.
My idea was to do the following, but I am receiving a System.NullReferenceException
as Booking is null:
@page
@model Onlinebooking.Web.Pages.BookingModel
@{
ViewData["Title"] = "Booking";
}
<h1>Booking4</h1>
<form method="post">
Reference: <Input asp-for="Booking.Reference" /><br />
Pieces: <input asp-for="Booking.BookingDetails[0].Pieces" /><br />
Marks: <input asp-for="Booking.BookingDetails[0].Marks" /><br />
<br />
<input type="submit" value="submit" />
</form>
<form method="post" asp-page-handler="DeleteBookingDetails">
@for (var i = 0; i < @Model.Booking.BookingDetails.Count(); i++)
{
@Html.LabelFor(x => x.Booking.BookingDetails[i].Pieces)
@Html.LabelFor(x => x.Booking.BookingDetails[i].Marks)
}
</form>
Is it even possible to do all that on one page or should I go in a different direction?
Thanks for your help!
EditorFor
... btw, why the-1
in@Model.Booking.BookingDetails.Count()-1
? – Stefan-1
is a mistake and makes no sense. I edited the post. – Matze Donien