【发布时间】:2020-05-27 19:25:13
【问题描述】:
我是 MVC 新手。我正在尝试开发汽车预订应用程序。我有一个名为 Reservations 的表和一个名为 Cars 的表。我必须按给定的位置、时间间隔(目前未预订的汽车)为用户显示可用的汽车。
我的想法是创建一个视图以从用户那里获取数据(位置、时间间隔),在他提交该数据后,他会被重定向到显示这些记录(汽车详细信息)的页面。问题是我真的不知道如何绑定视图以及如何从列表中显示。
这是我尝试过的:
我的控制器:
public ActionResult DisplayCars()
{
return View();
}
这里我试着列出一个列表并从数据库中添加结果记录
[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);
}
这是我从用户那里获取输入的视图:
@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>
这是显示记录的视图
@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>
这是我的汽车模型
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; }
}
}
这是预订模型
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; }
}
请帮助我一些提示或想法。谢谢!
【问题讨论】:
-
从结构上看你的代码,你为什么选择使用原始 SQL 查询而不是实体框架?
-
由于知识匮乏,刚开始接触实体框架,还不知道怎么用
标签: asp.net-mvc