删除具有自定义模型的用户,并且默认视图httppost模型为null

时间:2019-04-08 01:09:43

标签: c# asp.net model-view-controller asp.net-mvc-5 asp.net-identity

我只是尝试使用用户帐户的默认删除视图删除用户。我有一个带有用户详细信息的定制模型。我可以使用自定义模型中的正确数据加载第一个httpget delete视图,但是当我将其传递给delete httppost时,该模型为null。我尝试了多种不同的方法来使动作看到该模型,默认的BeginForm()在beginform中传递控制器和动作,在postform中通过post参数传递模型,控制器和动作。

P.S。 -如果我像这样传递ID(DeleteConfirmed(string id))并使用findbyidasync(id)再次获得用户,则可以像这样成功删除用户。为什么我不能通过模型​​?

这是我的代码

 // button that opens my delete view with the correct data.                                 @Html.ActionLink("Delete My Account", "DeleteUser", "Users",
     new { id = Model.UserId }, new { @class = "btn btn-danger" })                          //Delete View    @model MVC_TimeSh.Models.UpdateUserViewModel
@{ 
    ViewBag.Title = "Delete Account";
}
<h3>Are you sure you want to delete your account?</h3>
<div>
    <h4>Delete Confirmation</h4>
    <hr />
    <dl class="dl-horizontal">
        @*@Html.HiddenFor(u => u.UserId)*@
        <dt>@Html.DisplayNameFor(model => model.UserId) </dt>
        <dd>@Html.DisplayFor(model => model.UserId) </dd>
        <dt> @Html.DisplayNameFor(model => model.IdShortened) </dt>                      @* "DeleteUser", "Users", FormMethod.Post, Model *@
    @using (Html.BeginForm("DeleteUser", "Users", 
            new { UpdateUserViewModel = Model }, FormMethod.Post)){
        @Html.AntiForgeryToken()                                                       <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-danger"
                   onclick="return confirm('Confirmation of Delete Account?');" /> |
// GET: /User/Delete/{id}
        public async Task<ActionResult> DeleteUser(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var user = await UserManager.FindByIdAsync(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            var userRole = await UserManager.GetRolesAsync(user.Id);

            return View(new UpdateUserViewModel()
            {
                UserId = user.Id,
                IdShortened = user.Id.Substring(0, 10),
                Name = user.Name,
                PhoneNumber = user.PhoneNumber,
                Birthday = user.Birthday,
                DateCreated = user.DateCreated,
                Email = user.Email,
                UserName = user.UserName,
                UserRoles = userRole.FirstOrDefault()
            });
        }
        // POST: /User/Delete/{id}
        [HttpPost, ActionName("DeleteUser")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(UpdateUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                if(model == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                var user = await UserManager.FindByIdAsync(model.UserId);
                var logins = user.Logins;
                var roles = await UserManager.GetRolesAsync(user.Id);
                // Supposed to only be 1 role but just in case -
                if(roles.Count() > 0)
                {
                    foreach(var r in roles)
                    {
                        var deleteRole = await UserManager.RemoveFromRoleAsync(user.Id, r);
                    }
                }
                var result = await UserManager.DeleteAsync(user);
                if (result.Succeeded)
                {
                    TempData["Success"] = "User Deleted Successfully";
                    return RedirectToAction("UserDashboard", "Users");
                }                
            }
            TempData["Error"] = "User Delete Unsuccessful";
            return RedirectToAction("UserDashboard", "Users");
        }

0 个答案:

没有答案