179 Delete View

示例

Index.cshtml中添加Delete按钮

<td style="width:20%">
    <a asp-controller="Persons" asp-action="Edit" asp-route-personId="@person.PersonId">Edit</a>
    <a asp-controller="Persons" asp-action="Delete" asp-route-personId="@person.PersonId">Delete</a>
</td>

从Edit.cshtml拷贝一个Delete.cshtml,修改后内容如下

@model PersonResponse

@{
    ViewBag.Title = "Delete Person";
}

<a asp-controller="Persons" asp-action="Index" class="link-hover">Back to Persons List</a>

<h2>Delete Person</h2>
<h3>Are you sure to delete this person?</h3>

<div class="center-box">
    <form asp-controller="Persons" asp-action="Delete" method="post">
        @*PersonId*@
        <input asp-for="PersonId" type="hidden"/>
        @*PersonName*@
        <div class="form-field flex">
            <div class="w-25">
                <label asp-for="PersonName" class="form-label pt">Person Name</label>
            </div>
            <div class="flex-1">
                <input asp-for="PersonName" class="form-input" disabled="disabled"/>
                <span asp-validation-for="PersonName" class="text-red"></span>
            </div>
        </div>
        @*Email*@
        <div class="form-field flex">
            <div class="w-25">
                <label asp-for="Email" class="form-label pt"></label>
            </div>
            <div class="flex-1">
                <input asp-for="Email" type="email" class="form-input" disabled="disabled"/>
                <span asp-validation-for="Email" class="text-red"></span>
            </div>
        </div>
        
        @*SubmitButton*@
        <div class="form-field flex">
            <div class="w-25">
            </div>
            <div class="flex-1">
                <button class="button button-red-back">Delete</button>

                <div asp-validation-summary="All" class="text-red"></div>

                @if (ViewBag.Errors != null)

                {
                    <div class="text-red ml">
                        <ul>
                            @foreach (string error in ViewBag.Errors)
                            {
                                <li class="ml">@error</li>
                            }
                        </ul>
                    </div>
                }
            </div>
        </div>
        
        
    </form>
</div>

PersonsController.cs中添加如下代码

[HttpGet]
[Route("[action]/{personId}")]
public IActionResult Delete(Guid? personId)
{
    PersonResponse? personResponse = _personsService.GetPersonByPersonId(personId);
    if (personResponse == null)
    {
        return RedirectToAction("Index");
    }
    
    return View(personResponse);
}

[HttpPost]
[Route("[action]/{personId}")]
public IActionResult Delete(PersonUpdateRequest personUpdateRequest)
{
    if (personUpdateRequest == null)
    {
        return RedirectToAction("Index");
    }
    PersonResponse? personResponse = _personsService.GetPersonByPersonId(personUpdateRequest.PersonId);
    if (personResponse == null)
    {
        return RedirectToAction("Index");
    }

    _personsService.DeletePerson(personUpdateRequest.PersonId);
    return RedirectToAction("Index");
}

运行程序显示如下,可以编辑和删除

Gitee获取源码:

https://gitee.com/huang_jianhua0101/asp.-net-core-8.git

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值