'DateTime from view to control to Mapper
i'm trying to pass A DateTime data type to a controller that will call a Mapper to pass it into a db. Additionally the DateTime is taken from a db at first.
db > DateTime > View > Controller > Mapper > DB
The Mapper is suppose to put the data into the DB the issue is that all other data goes in properly except the DateTime.
I'm Using ASP.NET core 5 and SQL server
Model
using System;
using System.ComponentModel.DataAnnotations;
public class Housekeeping
{
[Key]
public int itemID { get; set; }
public string serviceName { get; set; }
public float serviceCost { get; set; }
public int customerID { get; set; }
public DateTime time { get; set; }
}
Controller code
public class HousekeepingController : Controller
{
// private readonly Data.DataMappers.HouseKeepingTablesMapper mapper2;
private readonly DAL.HouseKeepingMapper mapper;
private readonly HouseKeepingTableGateway TheHouseKeepingTableGateWay;
private readonly HouseKeepingGateway TheHouseKeepingGateWay;
[HttpPost]
public IActionResult Create(Housekeeping entity)
{
mapper.Insert(entity);
return RedirectToAction("Index", "Request");
}
}
Mapping Class
public class HouseKeepingMapper : IMapper<Housekeeping>
{
private readonly ApplicationDBContext _db;
public HouseKeepingMapper(ApplicationDBContext db)
{
_db = db;
}
public void Insert(Housekeeping entity)
{
var HousekeepingData = new GuestRequestTable();
HousekeepingData.ServiceName = entity.serviceName;
HousekeepingData.DateTime = entity.time;
HousekeepingData.Status = "Pending";
_db.GuestRequests.Add(HousekeepingData);
Save();
}
public void Save()
{
_db.SaveChanges();
}
}
Specifically this line is what passes the DateTime to the controller. The full code is bellow the code directly below.
<input type="submit" asp-controller="Housekeeping" asp-action="Create" asp-route-serviceName="@item.serviceName" asp-route-time="@item.time" value="Confirm Booking"/>
@model IEnumerable<ProjectConciergeM3.Models.HousekeepingTable>
@{
ViewData["Title"] = "Housekeeping Request Page";
IEnumerable<Housekeeping> bookingHousekeeping = ViewData["BookingHousekeeping"] as IEnumerable<Housekeeping>;
IEnumerable<HousekeepingTable> HouseKeepingList = ViewData["TheHouseKeepingList"] as IEnumerable<HousekeepingTable>;
}
<tbody>
@foreach(Housekeeping item in bookingHousekeeping)
{
<tr>
<td>
<b>@item.serviceName</b>
</td>
<td>
<b>[email protected]</b>
</td>
<td>
<b>@item.time</b>
</td>
<td>
<form method="post">
<input asp-controller="Housekeeping" asp-action="Delete" asp-route-Id="@item.itemID" type="submit" value="Cancel"/>
</form>
<form method="post">
<input type="submit" asp-controller="Housekeeping" asp-action="Create" asp-route-serviceName="@item.serviceName" asp-route-time="@item.time" value="Confirm Booking"/>
</form>
<br/>
</td>
</tr>
}
</tbody>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|