Reputation: 37
I'm trying to save my model into a SQL database, but after calling the method CreateReservation
, the database is still without data. I get no error.
public async Task CreateReservation(DateTime date, int _id, string name)
{
ReservationModel reservationModel = new ReservationModel { Cas = date, Name = name, Id = _id,Room= await _roomModel.GetRoom(_id) };
await _db.Reservations.AddAsync(reservationModel);
_db.SaveChanges();
}
Model
public class ReservationModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTime Cas { get; set; }
public RoomModel Room { get; set; }
public ReservationModel()
{
}
public ReservationModel(int id, string name, DateTime cas, RoomModel room)
{
Id = id;
Name = name;
Cas = cas;
Room = room;
}
}
Method is called from a controller:
[HttpPost]
[Route("Home/Room/Create_reservation")]
public IActionResult CreateReservation([Bind("Name")] ReservationModel reservationModel)
{
_reservation.CreateReservation((DateTime)TempData["Date"], (int)TempData["Id"],reservationModel.Name);
return RedirectToAction("Index");
}
Upvotes: 0
Views: 280
Reputation: 37
The problem was in action method. I had to add async await.
public async Task<IActionResult> CreateReservation([Bind("Name")] ReservationModel reservationModel)
{
await _reservation.CreateReservation(reservationModel,(DateTime)TempData["Date"], (int)TempData["Id"]);
return RedirectToAction("Index");
}
Upvotes: 1