asp.net mvc post post什么都没发布

时间:2012-12-19 10:11:34

标签: c# asp.net-mvc

我的行动方法:

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

Delete.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检查发布的数据,我发现此方法不发布任何内容到服务器。

任何建议?

2 个答案:

答案 0 :(得分:1)

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

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

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

Delete.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>
}

答案 1 :(得分:-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的部分添加隐藏输入,或者将部分保留在原来的位置,并在表单中放置隐藏的输入。

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