简体   繁体   中英

Handlers error using ASP.NET CORE 2 Razor Pages

i'm tryng to use a handler function in my ASP.NET Core 2 Razor Application, in my model i have this method:

public async Task<IActionResult> OnPostAddBookingAsync(int? id)
    {
        LoadAvailableRooms();

        TemporaryBookingTable tbt = new TemporaryBookingTable();

        var TemporaryBookingList = from c in _context.TemporaryBookingTables
                                   select c;

        if (!_context.TemporaryBookingTables.Any(t => t.RoomID == id))
        {
            tbt.CustomerID = 0;
            tbt.RoomID = Convert.ToInt16(id);
            tbt.UserID = 0;
            _context.TemporaryBookingTables.Add(tbt);
            await _context.SaveChangesAsync();
        }

        foreach (var item in TemporaryBookingList)
        {
            SelectedRoom = await _context.Rooms.FirstOrDefaultAsync(r => r.RoomID == item.RoomID);
            Room.Add(SelectedRoom);
        }

        return Page();
    }

What i'm try to do is... In my html razor page i have a list, and each item has a button that is going to delete the selected item.

Razor Page:

@{
    foreach (var item in Model.Room)
    {
<div>
    <h3>@Html.DisplayFor(modelItem => item.RoomType)</h3>
    <h4>@Html.DisplayFor(modelItem => item.RoomPrice)</h4>
    <form method="post">
        <input type="hidden" />
        <button type="submit" asp-page-handler="DeleteBooking" asp-route-id="@item.RoomID" class="btn btn-default btn-sm">Eliminar</button>
    </form>

</div>
    }

}

When i try to acces this page with url is: http://localhost:62755/admin/Booking

I get this error:

NullReferenceException: Object reference not set to an instance of an object.
SomethingNewDemo.Pages.admin.Booking_Page+<>c__DisplayClass17_1+<<ExecuteAsync>b__3>d.MoveNext() in Booking.cshtml
+
            <button type="submit" asp-page-handler="DeleteBooking" asp-route-id="@item.RoomID" class="btn btn-default btn-sm">Eliminar</button>
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext+<GetChildContentAsync>d__31.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper+<ProcessAsync>d__7.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner+<RunAsync>d__0.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
SomethingNewDemo.Pages.admin.Booking_Page+<ExecuteAsync>d__17.MoveNext() in Booking.cshtml
+
        <h4>@Html.DisplayFor(modelItem => item.RoomPrice)</h4>

Thanks for read, have you deal with problems like this?

You need to pass your model as parameter in your view action result otherwise the model data is not going to be sent to the view and you'll get the bill reference. Change the last line of your controller action to something like this:

//Assuming that TemporaryBookingTable is what you want to display return Page(TemporaryBookingTable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM