简体   繁体   English

脚手架实体框架 DeletePersonalData 不起作用 onclick

[英]Scaffolded Entity Framework DeletePersonalData not working onclick

I'm hoping someone can help me here.我希望有人可以在这里帮助我。 I scaffolded in the Entity Framework DeletePersonalData page to my project and changed some styling and layout, but I do not think I changed any of the default functionality.我在 Entity Framework DeletePersonalData 页面中搭建了我的项目并更改了一些样式和布局,但我认为我没有更改任何默认功能。

However, when the "Delete data and close my account" button is clicked nothing happens.但是,当单击“删除数据并关闭我的帐户”按钮时,没有任何反应。 My other scaffolded pages are working fine and changing the DB when required.我的其他脚手架页面工作正常,并在需要时更改数据库。

Also if it helps, when I enter no password and click the button nothing happens.另外,如果有帮助,当我没有输入密码并单击按钮时,什么也没有发生。 So the if (.await _userManager,CheckPasswordAsync(user. Input.Password)) is not working either.所以if (.await _userManager,CheckPasswordAsync(user. Input.Password))也不起作用。

Nothing at all is happening in the inspect tab of chrome dev tools when I click so the Post is not being triggered.当我单击时,chrome 开发工具的检查选项卡中什么都没有发生,因此不会触发帖子。

I did an add-migration and update-database just in case.为了以防万一,我做了一个添加迁移和更新数据库。 It didn't help.它没有帮助。

Using .Net Core 3.0使用.Net Core 3.0

Any ideas?有任何想法吗?

cshtml: cshtml:

                                <div class="col-lg-9 font-style-content">
                                <div class="form-group justify-content-center align-items-center">
                                    <br />
                                    <div asp-validation-summary="All" class="text-danger"></div>
                                    @if (Model.RequirePassword)
                                    {
                                        <div class="form-group justify-content-center align-items-center">
                                            <label class="font-heading" asp-for="Input.Password"></label>
                                            <input asp-for="Input.Password" class="form-control" />
                                            <span asp-validation-for="Input.Password" class="text-danger"></span>
                                        </div>
                                    }
                                    <button class="btn btn-outline-danger button-title" type="submit">Delete data and close my account</button>
                                </div>
                            </div>
@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

cshtml.cs: cshtml.cs:

using MyApp.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using LocSourceNameReferenceLibrary;
using Microsoft.Extensions.Logging;

namespace MyApp.Areas.Identity.Pages.Account.Manage
{
    public class DeletePersonalDataModel : PageModel
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _signInManager;
        private readonly ILogger<DeletePersonalDataModel> _logger;

public DeletePersonalDataModel(
            UserManager<ApplicationUser> userManager,
            SignInManager<ApplicationUser> signInManager,
            ILogger<DeletePersonalDataModel> logger)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
        }

        [BindProperty]
        public InputModel Input { get; set; }

        public class InputModel
        {
            [Required]
            [DataType(DataType.Password)]
            public string Password { get; set; }
        }

        public bool RequirePassword { get; set; }

        public async Task<IActionResult> OnGet()
        {
            public async Task<IActionResult> OnPostAsync()
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);
            return Page();
        }

        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);
            if (RequirePassword)
            {
                if (!await _userManager.CheckPasswordAsync(user, Input.Password))
                {
                    ModelState.AddModelError(string.Empty, "Incorrect password.");
                    return Page();
                }
            }

            var result = await _userManager.DeleteAsync(user);
            var userId = await _userManager.GetUserIdAsync(user);
            if (!result.Succeeded)
            {
                throw new InvalidOperationException($"Unexpected error occurred deleting user with ID '{userId}'.");
            }

            await _signInManager.SignOutAsync();

            _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);

            return Redirect("~/");
        }

I forgot the form method="post" tag in my cshtml file.我在我的 cshtml 文件中忘记了form method="post"标记。 oops!哎呀!

<form id="delete-account-form" method="post">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM