MVC5:删除身份用户导致参数ID的空入口?

时间:2014-06-05 14:50:19

标签: asp.net-mvc asp.net-mvc-5 http-post entity-framework-6 asp.net-identity

我在我的MVC5应用程序中为USERS创建了管理员编辑视图。在右下方我有一个DELETE按钮,首先要求管理员登录以确认用户删除,然后应该在我的UserManagement Controller中执行POST - Delete方法。但是当我确认删除时,我收到错误:

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>

这是我[HttpPost]行动的Delete

    [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);
    }

显然,我无法将ID从视图传递到Action,但我不太确定如何解决问题。我对MVC开发还很新,更不用说ASP.Net Identity了。任何帮助表示赞赏!

修改 我稍微调整了一下我的方法,但仍然遇到同样的错误。我有一个删除确认视图,它加载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>

}

我的控制器操作删除()

    //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

我不太确定为什么我的Model ID仍然以Delete()POST操作为止。

0 个答案:

没有答案