I am building a basic Car Rental Application. The user can view the cars and click the Rent
button. After clicking it, I need to return a new View
which contains a form, that the user has to complete in order to finish the order. I am having problems passing the Car
data as well as the Customer
data between the controllers in order to complete the Rent
.
On the main page, I have a Rent link under every car. Here is the code:
<div class="col-md-12">
<p>@Html.ActionLink("Rent", "Rent" , new { Id = car.Id})</p>
</div>
Rent
method from HomeController
public ActionResult Rent(string id)
{
return RedirectToAction("Create", "Rents");
}
Create
method from RentsController
[HttpPost]
public ActionResult Create(string carId, Rent rent)
{
if (!ModelState.IsValid)
return View();
var carToRent = context.Cars.SingleOrDefault(c => c.Id == carId);
if (carToRent == null)
return Content($"Car not found!");
rent.Car = carToRent;
var customer = context.Customers.SingleOrDefault(c => c.UserId == User.Identity.Name);
if (customer == null)
return Content($"Customer not found!");
rent.Customer = customer;
context.Rents.Add(rent);
context.SaveChanges();
return RedirectToAction("Index");
}
I am getting an HTTP 404 Error every time I try to access Rents/Create
.