I'm new with MVC. I'm trying to develop a car reservation application. I have a Table named Reservations and one named Cars. I have to display the available cars for the users by the given Location, Time interval(cars that are not reserved at the moment).
My idea is to make a view to get the data from the user(location, time interval) and after he submits that data, he get's redirected to the page that displays those records(car details). The problem is that I don't really know how to bind the Views and how to display from the list.
Here's what I tried:
My controller:
public ActionResult DisplayCars()
{
return View();
}
Here I've tried to make a list and add the resulted records from the database
[HttpPost]
public ActionResult AvailableCars([Bind(Include = "StartDate,EndDate,Location")] Reservations reservation)
{
List<Cars> carList = null;
if (ModelState.IsValid)
{
if (reservation.StartDate != null && reservation.EndDate != null && reservation.Location != null)
{
carList = db.Database.SqlQuery<Cars>("Select * from Cars WHERE Location = @location AND CarID NOT IN" +
"(Select CarID FROM Reservations WHERE NOT (StartDate > @endDate) OR (EndDate < @startDate))",
new SqlParameter("location", reservation.Location), new SqlParameter("endDate", reservation.EndDate), new SqlParameter("startDate", reservation.StartDate)).ToList<Cars>();
}
else if(reservation.StartDate == null && reservation.EndDate == null && reservation.Location != null)
{
carList = db.Database.SqlQuery<Cars>("Select * from Cars WHERE Location = @location",
new SqlParameter("location", reservation.Location)).ToList<Cars>();
}
else if(reservation.StartDate != null && reservation.EndDate != null && reservation.Location == null)
{
carList = db.Database.SqlQuery<Cars>("Select * from Cars WHERE CarID NOT IN" +
"(Select CarID FROM Reservations WHERE NOT (StartDate > @endDate) OR (EndDate < @startDate))",
new SqlParameter("endDate", reservation.EndDate), new SqlParameter("startDate", reservation.StartDate)).ToList<Cars>();
}
}
if(carList == null)
{
ModelState.AddModelError("", "No available cars");
}
return View(carList);
}
Here's my View for getting the input from the user:
@model RentC.UI.Models.Reservations
@{
ViewBag.Title = "DetailsAvailableCars";
}
<h2>DetailsAvailableCars</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Search" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "AvailableCars")
</div>
This is the View for displaying the records
@model IEnumerable<RentC.UI.Models.Cars>
@{
ViewBag.Title = "Available Cars List";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Plate)
</th>
<th>
@Html.DisplayNameFor(model => model.Manufacturer)
</th>
<th>
@Html.DisplayNameFor(model => model.Model)
</th>
<th>
@Html.DisplayNameFor(model => model.PricePerDay)
</th>
<th>
@Html.DisplayNameFor(model => model.Location)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Plate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Manufacturer)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model)
</td>
<td>
@Html.DisplayFor(modelItem => item.PricePerDay)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CarID }) |
@Html.ActionLink("Details", "Details", new { id=item.CarID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CarID })
</td>
</tr>
}
</table>
This is my Car Model
public partial class Cars
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Cars()
{
this.Reservations = new HashSet<Reservations>();
}
public int CarID { get; set; }
[Display(Name = "Cart Plate")]
public string Plate { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public decimal PricePerDay { get; set; }
public string Location { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Reservations> Reservations { get; set; }
}
}
This is the model for reservations
public partial class Reservations
{
public int ReservationID { get; set; }
public int CarID { get; set; }
public int CustomerID { get; set; }
public System.DateTime StartDate { get; set; }
public System.DateTime EndDate { get; set; }
public virtual Cars Cars { get; set; }
}
Please help me with some tips or ideas. Thank you!