传递到 ViewDataDictionary 的模型项属于 'Microsoft.AspNetCore.Identity.IdentityRole' 类型,但此 ViewDataDictionar



我想在我的RolesController中添加一个"delete"方法,但遇到了这个异常"InvalidOperationException:传递到ViewDataDictionary的模型项的类型为"Microsoft.AspNetCore.Identity.IdentityRole",但此ViewDataDiction实例需要类型为"SpaghettiOnline.Models.RoleEdit"的模型项;。有什么办法可以让我的视图正常工作吗??这是我的密码。。。

角色控制器.cs

//Get : Admin/Roles/Delete/id=?
public async Task<IActionResult> Delete(string id)
{
IdentityRole role = await roleManager.FindByIdAsync(id);
if (role == null)
{
return NotFound();
}
return View(role);
}

RoleEdit.cs

public class RoleEdit
{
public IdentityRole Role { get; set; }
public IEnumerable<AppUser> Members { get; set; }
public IEnumerable<AppUser> NonMembers { get; set; }
public string RoleName { get; set; }
public string[] AddIds { get; set; }
public string[] DeleteIds { get; set; }
}

删除.cshtml

@model RoleEdit
@{
ViewData["Title"] = "Delete Role";
}
<form asp-action="Delete" method="post">
<div class="container p-3">
<div class="border p-3">
<h1 class="h2 mb-3">Delete Role</h1>
<hr /><br />
<input name="RoleName" value="@Model.Role.Name" hidden />
<div>
<dl class="row">
<dt class="col-sm-2">
Role Name
</dt>
<dd class="col-sm-10">
@Model.Role.Name
</dd>
</dl>
</div>
<button id="confirm-deletion" type="submit" class="btn btn-danger" style="width:150px">Delete</button>
<a asp-controller="Categories" asp-action="Index" class="btn btn-light" style="width:150px">
Back to List
</a>
</div>
</div>
</form>

Delete.cshtml中,我刚刚修改了3行代码,它对我有效…

@model Microsoft.AspNetCore.Identity.IdentityRole

而不是

@model RoleEdit

<input asp-for="Id" hidden />

而不是

<input name="RoleName" value="@Model.Role.Name" hidden />

<button id="confirm-deletion" type="submit" class="btn btn-danger" style="width:150px" asp-route-id="@Model.Id">Delete</button>

刚刚将asp-route-id="@Model.Id"添加到"删除"按钮并且类似地将@Model.Role.Name改变为@Model.Name

谢谢你的建议,维克多

最新更新