Asp.net mvc MVC5:删除标识用户会导致参数ID为空条目吗?
我在MVC5应用程序中为用户创建了管理员编辑视图。在右下角我有一个删除按钮,它首先要求登录的管理员确认用户删除,然后在我的UserManagement控制器中执行POST-DELETE方法。但是,当我确认删除时,我得到一个错误:Asp.net mvc MVC5:删除标识用户会导致参数ID为空条目吗?,asp.net-mvc,asp.net-mvc-5,http-post,entity-framework-6,asp.net-identity,Asp.net Mvc,Asp.net Mvc 5,Http Post,Entity Framework 6,Asp.net Identity,我在MVC5应用程序中为用户创建了管理员编辑视图。在右下角我有一个删除按钮,它首先要求登录的管理员确认用户删除,然后在我的UserManagement控制器中执行POST-DELETE方法。但是,当我确认删除时,我得到一个错误: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Threading.Tas
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Delete(Int32)' in 'PROJECT.Areas.Admin.Controllers.UserManagementController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
这是视图上的“我的删除”按钮:
<div class="col-md-2" style="width:180px; float: right; padding-top: 12px; padding-bottom: 7px; border-bottom: 1px solid #dddddd; ">
@using (Html.BeginForm("Delete", "UserManagement", new { id = Model.Id }, FormMethod.Post))
{
@Html.AntiForgeryToken()
<input type="submit" class="btn btn-danger btn-xs" value="Delete User" onclick="return confirm('Are you sure you want to delete this User?');" />
}
</div>
显然,在某些地方,我无法将ID从视图传递到操作,但我不太确定如何解决该问题。我对MVC开发还是个新手,更不用说ASP.Net了。感谢您的帮助
编辑:
我已经对我的方法做了一些调整,但仍然得到相同的错误。我有一个删除确认视图,它加载Delete()GET方法。该视图类似于以下内容:
@model PROJECT.Models.ApplicationUser
@{
ViewBag.Title = "Delete";
Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";
string cancelEditUrl = "/Admin/UserManagement/";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this User?</h3>
@using (Html.BeginForm("Delete", "UserManagement", new { id = Model.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.ForumUsername)
@Html.HiddenFor(model => model.ReceiveSystemEmails)
@Html.HiddenFor(model => model.RegisteredDate)
@Html.HiddenFor(model => model.LastVisitDate)
@Html.HiddenFor(model => model.MemberOrgId)
@Html.HiddenFor(model => model.ProfilePictureSrc)
<div class="container">
<div class="row">
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="row">
<div class="editor-label">
@Html.LabelFor(model => model.Position)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Position, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Position)
</div>
</div>
.....
<div class="row">
<div class="col-md-1" style="padding-left: 0px">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
<div class="col-md-9" style="padding-left: 0px">
<a href="@cancelEditUrl" class="btn btn-danger">Cancel</a>
</div>
</div>
}
我不太清楚为什么我的模型ID在Delete()POST操作中仍然是空的。您能发布实际呈现的HTML表单吗?听起来id参数实际上没有包含在表单操作URI中。我尝试了一种新的方法,但得到的错误与以前基本相同。查看我的编辑。我请求呈现HTML输出的原因是为了验证
Id
属性中是否确实有值。你能发布呈现的输出吗?这样我们就可以准确地看到你的表单的样子,这样我们就知道HTTP请求最终是什么样子了。谢谢你回复Justin。我最终放弃了我的方法,使用标准切换到删除视图进行确认。不过,为了回答您的问题,我在ID
属性的其他字段中添加了LabelFor
和TextBoxFor
等,并可以确认该属性确实已填充且不为Null
。
@model PROJECT.Models.ApplicationUser
@{
ViewBag.Title = "Delete";
Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";
string cancelEditUrl = "/Admin/UserManagement/";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this User?</h3>
@using (Html.BeginForm("Delete", "UserManagement", new { id = Model.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.ForumUsername)
@Html.HiddenFor(model => model.ReceiveSystemEmails)
@Html.HiddenFor(model => model.RegisteredDate)
@Html.HiddenFor(model => model.LastVisitDate)
@Html.HiddenFor(model => model.MemberOrgId)
@Html.HiddenFor(model => model.ProfilePictureSrc)
<div class="container">
<div class="row">
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="row">
<div class="editor-label">
@Html.LabelFor(model => model.Position)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Position, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Position)
</div>
</div>
.....
<div class="row">
<div class="col-md-1" style="padding-left: 0px">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
<div class="col-md-9" style="padding-left: 0px">
<a href="@cancelEditUrl" class="btn btn-danger">Cancel</a>
</div>
</div>
}
//GET: Admin/UserManagement/Delete/5
public async Task<ActionResult> Delete(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ApplicationUser applicationUser = await UserManager.FindByIdAsync(id);
if (applicationUser == null)
{
return HttpNotFound();
}
return View(applicationUser);
}
// POST: Admin/UserManagement/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Delete(int id)
{
ApplicationUser applicationUser = db.Users.Find(id);
if (applicationUser == null)
{
ModelState.AddModelError("", "Failed to find User ID for deletion.");
}
else
{
IdentityResult result = await UserManager.DeleteAsync(applicationUser);
if (result.Succeeded)
{
await db.SaveChangesAsync();
return RedirectToAction("Index", "UserManagement");
}
else
{
ModelState.AddModelError("", "Failed to Delete User.");
var errors = string.Join(",", result.Errors);
ModelState.AddModelError("", errors);
}
}
return View(applicationUser);
}
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Delete(Int32)' in 'PRISMdev.Areas.Admin.Controllers.UserManagementController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters