【问题标题】:delete, edit, and details for a user in admin controller MVC 5管理员控制器 MVC 5 中用户的删除、编辑和详细信息
【发布时间】:2020-04-21 17:24:30
【问题描述】:

在我的应用程序中,我尝试添加创建、编辑、删除和详细信息操作。 Create 操作似乎工作得很好。但是,在删除操作中,我有以下内容:

 [HttpPost]
        public async Task<ActionResult> DeleteAspUsers(string id)
        {
            //this is the users model 
            AspUsers users = new AspUsers();
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);

            IdentityUser theUser = new IdentityUser() { Id = users.ID };

            theUser = userManager.FindById(theUser.Id);
            if (theUser != null)
            {
                IdentityResult result = await userManager.DeleteAsync(theUser);    

            }
            else
            {
                ModelState.AddModelError("", "user was not found");
            }

            return RedirectToAction ("GetAllUsers/");

        }

以及获取表中所有用户的视图如下:


@model IEnumerable<IdentityUser>

@using Microsoft.AspNet.Identity.EntityFramework;
@using Microsoft.AspNet.Identity.Owin;
@using Microsoft.AspNet.Identity;
@using RidesApp.Controllers;
@using RidesApp.Models;


@{
    ViewBag.Title = "GetAllUsers";
    Layout = "~/Views/Shared/AdminsLayout.cshtml";
}

<h2>GetAllUsers</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmailConfirmed)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PhoneNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PhoneNumberConfirmed)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TwoFactorEnabled)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LockoutEndDateUtc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LockoutEnabled)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AccessFailedCount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
<tr>

    <td>
        @Html.DisplayFor(modelItem => item.Id)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.EmailConfirmed)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PhoneNumber)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PhoneNumberConfirmed)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.TwoFactorEnabled)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LockoutEndDateUtc)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LockoutEnabled)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AccessFailedCount)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
        @Html.ActionLink("Details", "Details", new { id = item.Id }) |
        @Html.ActionLink("Delete", "DeleteAspUsers", new {id= item.Id})
    </td>
</tr>
    }

</table>

我试图使用我拥有的“AspUsers”模型,但出现错误,所以我尝试使用 (@model IEnumerable) 并且成功了。但是,当我单击删除时,我收到一个“404”错误,它没有找到。 我通常使用自己的 dtatbase(从 sql 服务器连接它)。我将身份添加到现有应用程序中。 所以,基本上我有自己的用户表,但现在我有 aspnetUsers 和添加身份框架后生成的其他表。这是我当前的表格:

我还想知道如何生成编辑并在“详细信息”操作中显示用户详细信息。

如果有任何帮助,我将不胜感激。谢谢。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-identity


    【解决方案1】:

    尝试使用

    return RedirectToAction ("GetAllUsers");
    

    而不是

    return RedirectToAction ("GetAllUsers/");
    

    【讨论】:

    • 其实url是(~/admin/GetAllUsers /"user's id"),没有找到的是user id。我相信它实际上根本不应该生成这个 url,因为它只是在删除用户。
    • 在这种情况下,它会返回 404(找不到页面),因为我们成功删除了用户,对吧? :) 我建议尝试创建一个无论请求是否成功都会返回响应的模型,并将其添加到重定向操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多