1

我的行动方法:

    [HttpPost, ActionName("Delete")]
    [RequiredPermissions(RequiredPermissionName, Operation.Delete)]
    public ActionResult DeleteConfirmed(User userWhoGone)
    {
        _db.Users.Remove(userWhoGone);
        _db.SaveChanges();

        this.TempData["msg"] = "Deleted User Id " + userWhoGone.Id;
        return RedirectToAction("Index");
    }

删除.cshtml

@model NewsMonitoringWeb.Database.Models.User
@{
    ViewBag.Title = "User Delete";

<h2>User Delete</h2>
<h3>Are you sure you want to delete this user?</h3>

@Html.Partial("_PartialUser")

@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}

_PartialUser.cshtml

@model NewsMonitoringWeb.Database.Models.User

<fieldset>
    <legend>User</legend>

    <div class="display-label">User Role</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.UserRole.Name)

    </div>

    <div class="display-label">FirstName</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.FirstName)
    </div>

    <div class="display-label">LastName</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.LastName)
    </div>

    <div class="display-label">Email</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Email)
    </div>

    <div class="display-label">Is Contributor</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.IsContributor)
    </div>
</fieldset>

我使用 chrome 检查发布的数据,我发现这种方法没有向服务器发布任何内容。

有什么建议吗?

4

2 回答 2

1

您的表单没有要回发的值。您需要在表单中放置一些隐藏的输入值来构建用户,或者向表单添加隐藏的用户 ID 并更改发布操作方法。

后者将是我的首选方法,如下所示:

[HttpPost, ActionName("Delete")]
[RequiredPermissions(RequiredPermissionName, Operation.Delete)]
public ActionResult DeleteConfirmed(int userIdWhoGone)
{

    _db.Users.RemoveById(userIdWhoGone); // You may need to create this.
    // Atlernatively, get the user by id then call 'Remove()
    var user = _db.User.Find(userIdWhoGone);
    _db.Users.Remove(user);


    _db.SaveChanges();

    this.TempData["msg"] = "Deleted User Id " + userIdWhoGone;
    return RedirectToAction("Index");
}

删除.cshtml

@model NewsMonitoringWeb.Database.Models.User
@{
    ViewBag.Title = "User Delete";

<h2>User Delete</h2>
<h3>Are you sure you want to delete this user?</h3>

@Html.Partial("_PartialUser")

@using (Html.BeginForm()) {
    <p>
        @Html.HiddenFor(m => m.Id) @* <--- New input *@
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
}
于 2012-12-19T12:21:35.803 回答
-1

您目前根本没有发布任何内容。您的用户部分在表单之外。

表单中唯一的内容是您的按钮和链接。

您需要移动您的部分,使其位于表单内。

所以它现在看起来像这样:

@model NewsMonitoringWeb.Database.Models.User
@{
ViewBag.Title = "User Delete";

<h2>User Delete</h2>
<h3>Are you sure you want to delete this user?</h3>



@using (Html.BeginForm()) {
@Html.Partial("_PartialUser")    
<p>
    <input type="submit" value="Delete" /> |
    @Html.ActionLink("Back to List", "Index")
</p>
}

编辑

您将需要向包含用户 ID 的部分添加隐藏输入,或者将部分保留在原处,然后在表单中放置隐藏输入。

然后,您需要更改操作以接受用户 ID 并在数据库上进行查找。

于 2012-12-19T12:17:45.540 回答