【问题标题】:Deleting piece of data from two different view-models从两个不同的视图模型中删除一条数据
【发布时间】: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


    【解决方案1】:

    我知道我需要将两个 id 传递给删除操作,但我不确定如何通过部分视图传递乘客 ID。

    &lt;partial&gt; 标签助手具有view-data 属性以将数据传递到部分视图。因此,在您的乘客详细信息视图中,将调用修改为部分视图:

    <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 })
    

    【讨论】:

    • 感谢工作。我也想出了如何说确认删除。但谢谢你分配
    【解决方案2】:

    这是一个使用 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));
    }
    

    【讨论】:

    • 抱歉,我看到我不清楚的地方,我编辑了帖子以更清楚我想要寻求帮助的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多