I have a controller function, that adds data to the database, I am passing the data to a view which contains the details of the form submitted. I would like to pass the Identity ID generated as query string so if somebody refreshes the page, I have the ID of the details that were added.
I have tried to add it to form post parameters but it does not work
Controller Function bvm.Booking has Identity Column BookingID that gets generated.
public ActionResult RequestBookingCustomer(BookingViewModel bvm)
{
_context.Booking.Add(bvm.Booking);
_context.Messages.Add(bvm.Messages);
_context.PetInformation.Add(bvm.Pets);
_context.SaveChanges();
return View("BookingDetails", bvm);
}
Booking Details View has simple Details page. The url on this page is www.xxx.com/Booking/RequestBookingCustomer with no parameters
<div class="profile-services">
@*Show all the booking details here*@
<span>Name:@Html.DisplayFor(model => model.Name)</span>
<br />
<span>Weight:@Html.DisplayFor(model => model.Weight)</span>
<br />
<span>Age:@Html.DisplayFor(model => model.AgeYears) Years</span>
<br />
</div>
How do I pass in the ID generated as query string so if someone refreshes the page, it calls RequestBookingCustomer with some ID that I can check so it does add to the database again?
What am I doing wrong and how can this be fixed in MVC controller to view?
you can use RedirectToAction method and pass BookingId as query string param to it as following:
public ActionResult RequestBookingCustomer(BookingViewModel bvm)
{
_context.Booking.Add(bvm.Booking);
_context.Messages.Add(bvm.Messages);
_context.PetInformation.Add(bvm.Pets);
_context.SaveChanges();
// redirect to get booking details action medthod
return RedirectToAction("BookingDetails", new { BookingId = bvm.BookingId});
}
then inside BookingDetails
get action method load booking data according to this parameter as following:
public ActionResult BookingDetails (int BookingId)
{
// load booking data for this BookingId
var BookingData = _context.Booking.Find(BookingId);
return view(BookingData);
}
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.