我有航班时刻表 - 航班列表的航班时刻表 我只返回View并进行一些过滤和排序。
以下是我观点的一部分:
@model IEnumerable<AirPortIS.Models.Flight>
<div class="page-header">
<h3>Flights</h3>
</div>
<div id="modDialog" class="modal fade">
<div id="dialogContent" class="modal-dialog"></div>
</div>
<table class="table-bordered table-condensed">
<thead>
<tr>
<th>Flight №</th>
<th>Departure</th>
<th>Destination</th>
<th>@Html.ActionLink("Day", "Schedule", new { sort = ViewBag.SortDay, company = ViewBag.FiltrC, destination = ViewBag.FiltrD })</th>
<th>Departure Time</th>
<th>Arrival Time</th>
<th>Company</th>
<th>@Html.ActionLink("Seats", "Schedule", new { sort = ViewBag.SortSeats, company = ViewBag.FiltrC, destination = ViewBag.FiltrD })</th>
<th>@Html.ActionLink("Cost", "Schedule", new { sort = ViewBag.SortCost, company = ViewBag.FiltrC, destination = ViewBag.FiltrD })</th>
<th>Book ticket</th>
</tr>
</thead>
@foreach (var f in Model)
{
<tr>
<td>@Html.ActionLink(f.FlightId.ToString(), "FlightDetails", new { id = f.FlightId }, new {@class = "flItem" } )</td>
<td>@f.Departure</td>
<td>@f.Destination</td>
<td>@f.Day</td>
<td>@f.DepartureTime</td>
<td>@f.ArrivalTime</td>
<td>@f.Company.Name</td>
<td>@f.Seats</td>
<td>@f.Cost</td>
<td>@Html.ActionLink("Link for booking ticket")</td>
</tr>
}
</table>
我需要通过单击“预订票证”按钮来实现这一点,用户正在获取一个页面,其中dropdownlist具有预设值FlightId。 例如,我们有一个航班№1和一个链接“预订机票”,所以当用户进入预订机票页面时,他会获得一个预选值为“1”的下拉列表
这是我的票模型
public class Tickets
{
public int Id { get; set; }
public int TicketId { get; set; }
public Flight Flight { get; set; }
public string Seat {get;set; }
public string Passenger { get; set; }
public int Flightid { get; set; }
public string Status { get; set; }
}
TicketsController的一部分:
public class TicketsController : Controller
{
private readonly AirportContext _db = new AirportContext();
[Authorize]
public ActionResult Tickets()
{
var ticket = _db.Tickets.Include(t => t.Flight);
return View(ticket);
}
[HttpGet]
public ActionResult BookTicket()
{
IEnumerable<SelectListItem> statusList = new SelectList(new List<string> { "Book", "Buy" });
IEnumerable<SelectListItem> flights = new SelectList(_db.Flights.ToList(), "FlightId", "FlightId");
ViewData["flights"] = flights;
ViewData["statusList"] = statusList;
return View();
}
[HttpPost]
public ActionResult BookTicket(Tickets ticket)
{
IEnumerable<SelectListItem> statusList = new SelectList(new List<string> { "Book", "Buy" });
IEnumerable<SelectListItem> flights = new SelectList(_db.Flights.ToList(), "FlightId", "FlightId");
ViewData["flights"] = flights;
ViewData["statusList"] = statusList;
foreach (var c in _db.Tickets.ToList())
{
if ((_db.Tickets.ToList().Exists(x => c.TicketId == ticket.TicketId)) || (ticket.TicketId <= 0))
{
ModelState.AddModelError("TicketId", "Wrong ticket id");
}
if ((_db.Tickets.ToList().Exists(x => c.Seat == ticket.Seat)) && (_db.Tickets.ToList().Exists(x => c.Flightid == ticket.Flightid))
&& (_db.Tickets.ToList().Exists(x => c.TicketId == ticket.TicketId)))
{
ModelState.AddModelError("Seat", "The seat is unavailable");
}
if (_db.Tickets.ToList().Exists(x => c.Passenger == ticket.Passenger))
{
ModelState.AddModelError("Passenger", "The ticket has already bought");
}
}
if (ModelState.IsValid)
{
_db.Tickets.Add(ticket);
_db.SaveChanges();
return RedirectToAction("Tickets");
}
else return View(ticket);
}
我的BookTikcet观点:
@model AirPortIS.Models.Tickets
@{
ViewBag.Title = "Book ticket";
}
<h2>Book ticket:</h2>
<form class="form-inline" method="post">
<div>
@Html.ValidationSummary()
</div>
<div class="form-group col-md-2">
Ticket №<br/>
@Html.EditorFor(model => Model.TicketId)
</div>
<div class="form-group col-md-1">
Flight №<br />
@Html.DropDownListFor(model => Model.Flightid, ViewData["flights"] as IEnumerable<SelectListItem>)
</div>
<div class="form-group col-md-2">
Место<br />
@Html.EditorFor(model => Model.Seat)
</div>
<div class="form-group col-md-2">
Passenger Name<br />
@Html.EditorFor(model => Model.Passenger)
</div>
<div class="form-group col-md-2">
Status<br />
@Html.DropDownListFor(model => Model.Status, ViewData["statusList"] as IEnumerable<SelectListItem>)
</div>
<div>
<input class="btn-success" type="submit" value="Book Ticket"/>
</div>
</form>
<div>
<form method="get" action="Tickets">
<button class="btn-danger" type="submit">Cancel</button>
</form>
</div>
我不知道该怎么做,所以上面的整个代码只是一个用于创建新票证的标准代码。 我应该如何修改代码或添加一些东西(例如我们有一个航班№1和一个链接“预订机票”,所以当用户进入预订机票页面时,他获得一个预选值为“1”的下降列表,用于航班№对于FlightId,页面下拉列表中的2具有预选值“2”。 希望我的问题清楚,对不起,如果写错了或不太清楚。
答案 0 :(得分:0)
您需要将FlightId
的值作为路由(或查询字符串)值传递给BookTicket
方法。你的链接应该是
@Html.ActionLink("Book ticket", "BookTicket", new { id = f.FlightId })
并将方法修改为
[HttpGet]
public ActionResult BookTicket(int ID)
{
... // set you SelectLists as above
// Initialize your model and set the Flightid property
var model = new Tickets()
{
Flightid = ID
};
return View(model); // return the model to the view
}
您的下拉列表现在将在您首次生成视图时选择Flightid标识的选项。
请注意。我建议您使用视图模型而不是Tickets
数据模型,该模型将包含属性IEnumerable<SelectListItem> Flights
和IEnumerable<SelectListItem> StatusList
,而不是使用ViewData
,以便您的视图使用
@Html.DropDownListFor(m => m.Flightid, Model.Flights)
您还应该考虑重构代码以将SelectLists
填充到私有方法中,以便您不重复代码,例如
private void ConfigureViewModel(TicketVM model)
{
model.Flights = new SelectList(...);
model.StatusList = new SelectList(...);
}
另请注意,如果SelectList
有效,则调用数据库以获取POST方法中的ModelState
是浪费资源。你的代码应该是
if (!ModelState.IsValid)
{
ConfigureViewModel(model); // only necessary if you need to return the view
return View(model);
}
// save and redirect
附注:不清楚为什么在Flightid
视图中确实需要BookTicket
的下拉列表。用户已选择航班,为什么您可以选择更改航班?将Flightid
渲染为隐藏或只读输入可能更合适,因此将其提交回POST方法。