Mostly working create rent. Awaiting migration

This commit is contained in:
Dimitar Byalkov
2022-04-09 00:16:58 +03:00
parent 91e835f63d
commit c04ffc5020
3 changed files with 27 additions and 34 deletions

View File

@@ -12,22 +12,12 @@ namespace Data.Entities
[Required]
[Key]
public int Id { get; set; }
[Required]
public string Brand { get; set; }
[Required]
public string Model { get; set; }
[Required]
public int Year { get; set; }
[Required]
public int CountPassengerSeats { get; set; }
public string Description { get; set; }
[Required]
public decimal PriceForDay { get; set; }
}
}

View File

@@ -59,12 +59,14 @@ namespace WebApp.Controllers
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public async Task<IActionResult> Create([Bind("CarId,StartDate,EndDate")] Rents rents)
public async Task<IActionResult> Create(Rents rents)
{
if (ModelState.IsValid)
{
var car = _context.Cars.FirstOrDefault(car => car.Id == 1);
rents.Car = car;
var user = _context.Users.FirstOrDefault(user => user.UserName == User.Identity.Name);
rents.User = user;
_context.Add(rents);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
@@ -95,13 +97,8 @@ namespace WebApp.Controllers
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int id, [Bind("CarId,StartDate,EndDate")] Rents rents)
public async Task<IActionResult> Edit(Rents rents)
{
if (id != rents.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try

View File

@@ -23,6 +23,9 @@
<th>
End Date
</th>
<th>
User
</th>
@if (this.User.IsInRole("Admin"))
{
<th>
@@ -34,25 +37,28 @@
<tbody>
@foreach (var item in Model)
{
<tr>
<tr>
<td>
@Html.DisplayFor(modelItem => item.Car.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.User.UserName)
</td>
@if (this.User.IsInRole("Admin"))
{
<td>
@Html.DisplayFor(modelItem => item.Car.Id)
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.EndDate)
</td>
@if (this.User.IsInRole("Admin"))
{
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
}
</tr>
}
</tr>
}
</tbody>
</table>