删除网格中的行后无法刷新页面

时间:2011-10-24 15:13:24

标签: asp.net-mvc

任何人都可以看到为什么这段代码不起作用? 我知道有人会注意到我正在使用删除链接,我应该使用DELETE动词而不是POST,但即使在SO的帮助下,我也无法解决该问题。 这里没有问题是我点击删除,基础数据被删除OK,但是在我删除数据后,当我尝试重定向到Payroll GET方法时,它不会被调用,因此屏幕无法获取刷新。 所以这是控制器上的代码;

[HttpGet]
[Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")]
public ActionResult Payroll()
{
    if ((SessionObjects.PeriodStartDate > DateTime.MinValue) && (SessionObjects.PeriodEndDate > DateTime.MinValue))
        if (SessionObjects.PayrollSelectedEmployeeId == 0)
            return View(new PayrollViewModel()
                {
                    PeriodStartDate = SessionObjects.PeriodStartDate,
                    PeriodEndDate = SessionObjects.PeriodEndDate
                });
        else
            return View(new PayrollViewModel(
                SessionObjects.PeriodStartDate,
                SessionObjects.PeriodEndDate,
                SessionObjects.PayrollSelectedEmployeeId
                ));

    return View();
}

[HttpPost]
[Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")]
public ActionResult Payroll(PayrollViewModel _pvm)
{
    if (ModelState.IsValid)
    {
        SessionObjects.PeriodStartDate = _pvm.PeriodStartDate;
        SessionObjects.PeriodEndDate = _pvm.PeriodEndDate;
        if (_pvm.SearchTextId > 0)
            SessionObjects.PayrollSelectedEmployeeId = _pvm.SearchTextId;
        return RedirectToAction("Payroll");
    }
    return View(_pvm);
}

//[AcceptVerbs(HttpVerbs.Delete)]
[HttpPost]
[Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")]
public RedirectToRouteResult Delete(int id)
{
    EmployeeOtherLeaf.Delete(id);
    return RedirectToAction("Payroll");
}

视图和编辑器模板的一部分;

    <table class="groupBorder">
        <tr>
            <th></th>
            <th>Leave Type</th>
            <th>Notes</th>
            <th>Day Amount</th>
            <th>Date</th>
            <th>Approver</th>
        </tr>
        <%: Html.EditorFor(x => x.LeaveList)%>
    </table>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.Leave>" %>
<%@ Import Namespace="SHP.WebUI.HtmlHelpers" %>
<%@ Import Namespace="SHP.Models" %>

<%: Html.RowStyle(Model.RowColour) %>
<tr>
    <td style="background-color:White;">
        <%-- Ajax Delete --%>
        <% if(Model.LeaveId > 0) { %>
        <%: Html.DeleteEmployeeOtherLeave()%>
        <%} %>
    </td>
    <td><%: Model.LeaveType %></td>
    <td><%: Model.Notes %></td>
    <td><%: Model.DayAmount %></td>
    <td><%: String.Format("{0:ddd MMM d yyyy}", Model.Date)%></td>
    <td><%: Model.ApproverName %></td>
</tr>
</tbody>    <%-- Leave this here, it closes from the above Html.RowStyle!--%>

HTML Helper方法;

public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper<Leave> html)
{
    var leave = html.ViewData.Model;
    return html.RouteLink(
        "Delete",
        "Default",
        new {id = leave.LeaveId, action = "Delete" },
        new { onclick = "return DeleteRow(this);" }
    );
}

1 个答案:

答案 0 :(得分:1)

您似乎在DeleteRow javascript函数(未显示)中使用AJAX调用Delete操作。您无法在AJAX请求中重定向。这就是他们的全部要点:不要刷新整个浏览器,而只刷新部分浏览器。

如果您想在AJAX调用的成功回调中执行完全重定向,可以使用window.location.href属性,如下所示:

success: function(result) {
    window.location.href = '/somecontroller/Payroll';
}

当然,做这样的事情毫无意义。我只想使用一个标准的HTML表单,它将发布删除操作,并且根本不使用任何javascript:

<% if(Model.LeaveId > 0) { %>
    <% using (Html.BeginForm("Delete", "Home", new { id = leave.LeaveId })) { %>
        <button type="submit">Delete</button>
    <% } %>
<% } %>

现在提交表单时,将调用Delete操作,该操作将执行实际删除并将浏览器重定向到Payroll操作=&gt;非常标准的HTTP对话。

如果您决定采用这种方式,您甚至可以获得奖励:您可以使用[HttpDelete]属性修饰您的控制器操作并在客户端上使用某种技术:

<% if(Model.LeaveId > 0) { %>
    <% using (Html.BeginForm("Delete", "Home", new { id = leave.LeaveId })) { %>
        <%= Html.HttpMethodOverride(HttpVerbs.Delete) %>
        <button type="submit">Delete</button>
    <% } %>
<% } %>

然后:

[HttpDelete]
[Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")]
public RedirectToRouteResult Delete(int id)
{
    EmployeeOtherLeaf.Delete(id);
    return RedirectToAction("Payroll");
}

在幕后它不是真正的DELETE HTTP动词,因为浏览器不支持表单,但它使用ASP.NET MVC理解的隐藏字段来模拟它,并且能够正确地将请求重新分配给相应的操作。

相关问题