代码如下:
1)控制器:
public async Task<ActionResult> Delete(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = await UserManager.FindByIdAsync(id);
if (user == null)
{
return HttpNotFound();
}
return PartialView(user);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{
if (ModelState.IsValid)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = await UserManager.FindByIdAsync(id);
if (user == null)
{
return HttpNotFound();
}
// Remove all the User Group references:
await this.GroupManager.ClearUserGroupsAsync(id);
// Then Delete the User:
var result = await UserManager.DeleteAsync(user);
if (!result.Succeeded)
{
ModelState.AddModelError("", result.Errors.First());
return PartialView();
}
return RedirectToAction("Index");
}
return PartialView();
}
2)删除视图:
@model StatusZN.Models.ApplicationUser
@{
ViewBag.Title = "Delete";
}
<h2>Delete.</h2>
<h3>Are you sure you want to delete this User?</h3>
<div>
<h4>User.</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.UserName)
</dt>
<dd>
@Html.DisplayFor(model => model.UserName)
</dd>
<dd>
@Html.DisplayFor(model => model.Id)
</dd>
</dl>
@using (Ajax.BeginForm("Delete", "UsersAdmin", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "UsersDiv"}, new { id = Model.Id}))
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@*@Html.ActionLink("Back to List", "Index")*@
<a href="#" onclick="BackToUsersList()">Back to List</a>
</div>
}
</div>
Visualy看起来如下:
问题是程序流进入DeleteConfirmed操作,但参数id为null。
如何传递模型中存在的参数?
答案 0 :(得分:0)
您使用错误的Ajax.BeginForm()
重叠并添加length=10
作为路由值(因为"UsersAdmin"
有10个字符)而adding
id
为<form>
您的@using (Ajax.BeginForm("Delete", "UsersAdmin", new { id = Model.Id }, new AjaxOptions { ....} ))
标记的html属性。您需要使用this overload,所以
routeValues
其中第3个参数是.map()