从两个不同的视图模型中删除数据

Deleting piece of data from two different view-models

所以我想删除列表中的一条数据。这样一个更好解释的例子是:

一位乘客有一个预订的航班列表。现在在乘客页面上,我可以选择查看他们的详细信息。所以我进入那里,我看到了预订航班的列表。但我需要删除他的一个预订。

问题来了。我不确定该怎么做,因为数据需要两个不同的数据集。

详细信息页面包含乘客的详细信息,还显示了他所有预订的部分视图。这是一些代码。

乘客详情视图:

@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 table 正在使用的数据。一个是以 IdC 作为主键的乘客 table,两个是以 IdF 作为主键的航班 table,最后一个 table 是航班预订 table (我知道我应该在我的代码中这样说)它有一个复合键,即 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; }
    }
}

这里是一个使用FlightsViewModel.Id属性删除航班预订的例子。

注意:如果 FlightsViewModel.Id 是 Fl​​ightBooking 实体中的主键,则此示例有效。如果主键不是Id,请详细说明FlightsViewModel和FlightBooking实体的定义。

祝你好运。

添加名为 DeleteF.cshtml

的视图
@model FlightsViewModel

<h1>Delete</h1>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Flight @Model.Id</h4>
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Id)
        </dt>
        <dt class = "col-sm-10">
            @Html.DisplayFor(model => model.Id)
        </dt>      
    </dl>

    <form asp-action="DeleteF">
        <input type="hidden" asp-for="Id" />
        <input type="submit" value="Delete" class="btn btn-danger" />
    </form>
</div>

在您的局部视图中为您的标记试试这个。

@model IEnumerable<WebSite.Models.PassengerViewModels.FlightsViewModel>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </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.Id)
            </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>
                @*Passing Id to DeleteF action*@
                @Html.ActionLink("Delete", "DeleteF", new { Id = item.Id })
            </td>
        </tr>
}
    </tbody>
</table>

在你的控制器中试试这个删除方法。

public async Task<IActionResult> DeleteF(int Id)
{

    var flightBooking = await _context.Flights
    .Select(a => new FlightsViewModel
    {
        Id = a.Id
        IdC = a.CustomerId,
        Email = a.Customer.Email,
        IdE = a.EventId,
        Title = a.Event.FNumber,
        Attended = a.Attended
    }).FirstOrDefaultAsync(e => e.Id == Id);


    if (flightBooking == null)
    {
        return NotFound();
    }

    return View(flightBooking);
}        

[HttpPost, ActionName("DeleteF")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmedF(FlightsViewModel model)
{
    var flightBooking = await _context.Flights.FindAsync(model.Id);
    _context.Flights.Remove(flightBooking);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}

I know i need to pass two id's into the delete operation but i am not sure how to pass the passenger ID through the partial view.

<partial> 标签助手具有 view-data 属性,可将数据传递给分部视图。因此,在您的 Passenger Details View 中,修改对部分视图的调用:

<partial name="_Flights" model=" @Model.Flights" view-data='@new ViewDataDictionary(ViewData) { { "IdC", Model.IdC } }' />

在您的部分视图中,您可以使用 ViewBag.IdCViewData["IdC"].

访问 IdC

IdC 传递给 DeleteF 操作:

@Html.ActionLink("Delete", "DeleteF", new {IdC = ViewBag.IdC, IdF = item.IdF })