【发布时间】: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