【发布时间】:2020-01-05 19:21:32
【问题描述】:
所以我想删除列表中的一条数据。这样一个更好地解释它的例子是:
一位乘客有一个已预订航班的列表。现在在乘客页面上,我可以选择查看他们的详细信息。所以我进入那里,我看到了预订航班的列表。但随后我需要删除他的一项预订。
这就是问题所在。我不确定该怎么做,因为数据需要两个不同的数据集。
详细信息页面包含乘客的详细信息,还显示了他所有预订的部分视图。这是一些代码。
乘客详情视图:
@model WebSite.Models.PassengerViewModels.PassengerDetialsViewModel
@{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<div>
<h4>PassengerDetailsViewModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.IdC)
</dt>
<dd>
@Html.DisplayFor(model => model.IdC)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Surname)
</dt>
<dd>
@Html.DisplayFor(model => model.Surname)
</dd>
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Email)
</dt>
<dd>
@Html.DisplayFor(model => model.Email)
</dd>
</dl>
</div>
<h4>Number of Flight: @Model.Flight.Count()</h4>
<partial name="_Flights" for="Flights" />
<div>
@Html.ActionLink("Edit", "Edit", new { id = Model.IdC }) |
<a asp-action="Index">Back to List</a>
</div>
那么我的部分观点是这样的:
@model IEnumerable<WebSite.Models.PassengerViewModels.FlightsViewModel>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.IdF)
</th>
<th>
@Html.DisplayNameFor(model => model.FNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Duration)
</th>
<th>
@Html.DisplayNameFor(model => model.Destination)
</th>
<th>
@Html.DisplayNameFor(model => model.Attended)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.IdF)
</td>
<td>
@Html.DisplayFor(modelItem => item.FNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Duration)
</td>
<td>
@Html.DisplayFor(modelItem => item.Destination)
</td>
<td>
@Html.DisplayFor(modelItem => item.Attended)
</td>
<td>
@Html.ActionLink("Delete", "DeleteF", new { Id = item.IdF })
</td>
</tr>
}
</tbody>
</table>
那么这里是控制器中的Delete操作,叫做DeleteF:
public async Task<IActionResult> DeleteF(int? IdC, int? IdF)
{
if (IdC == null && IdF == null)
{
return NotFound();
}
var flightBooking = await _context.Flights
.Select(a => new FlightsViewModel
{
IdC = a.IdC,
Email = a.Passenger.Email,
IdE = a.IdF,
Title = a.Flight.FNumber,
Attended = a.Attended
}).FirstOrDefaultAsync(e => e.IdC == IdC);
if (flightBooking == null)
{
return NotFound();
}
return View(flightBooking);
}
[HttpPost, ActionName("DeleteF")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmedF(int? IdC, int? IdF)
{
var flightBooking = await _context.Flights.FindAsync(IdC, IdF);
_context.Guests.Remove(flightBooking);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
我知道我需要将两个 id 传递给删除操作,但我不确定如何通过部分视图传递乘客 ID。
编辑:
航班预订数据位于其自己的列表中,该列表具有乘客和航班的复合键。这就是为什么我需要使用航班 ID 传递乘客 ID。
如果有帮助的话,这里还有删除页面:
@model WebSite.Models.PassengerViewModels.FlightsViewModel
@{
ViewData["Title"] = "DeleteF";
}
<h2>DeleteB</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>FlightsViewModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.IdC)
</dt>
<dd>
@Html.DisplayFor(model => model.IdC)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Email)
</dt>
<dd>
@Html.DisplayFor(model => model.Email)
</dd>
<dd>
@Html.DisplayFor(model => model.IdF)
</dd>
<dt>
@Html.DisplayNameFor(model => model.IdE)
</dt>
<dt>
@Html.DisplayNameFor(model => model.FNumber)
</dt>
<dd>
@Html.DisplayFor(model => model.FNumber)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Attended)
</dt>
<dd>
@Html.DisplayFor(model => model.Attended)
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="IdC" />
<input type="hidden" asp-for="IdF" />
<input type="submit" value="Delete" class="btn btn-default" /> |
<a asp-action="Index">Back to List</a>
</form>
</div>
澄清事情。从技术上讲,我正在使用 3 个数据表。一个是以 IdC 作为其主键的乘客表,两个是以 IdF 作为其主键的航班表,最后一个表是具有复合键的航班预订表(我知道我应该在我的代码中写这个词)即 IdC 和 IdF。
编辑 2:
这是乘客的视图模型:
namespace WebSite.Models.PassengerViewModels
{
public class PassengerDetialsViewModel
{
[Display(Name ="Passenger ID ")]
public int IdC { get; set; }
[Display(Name ="Surname ")]
[Required]
public string Surname { get; set; }
[Display(Name ="First Name ")]
[Required]
public string FirstName { get; set; }
[Display(Name ="E-mail Address ")]
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public IEnumerable<FlightsViewModel> Events { get; set; }
}
}
【问题讨论】:
标签: c# html asp.net-core asp.net-core-mvc viewmodel