0

当我在下面使用 [HttpPost] 定义我的 Delete 方法时,无法从视图中调用 Delete 方法。但是,当删除 [HttpPost] 行时,它可以正常工作。我尝试了很多事情,实际上这可能与我的视图中@Html.Hidden@using (Html.BeginForm()的错误使用有关。那么,您能否在下面澄清一下这些要点?

1) 单击 WebGrid 上的删除按钮后,我没有打开视图。在确认方法之后,应该调用Controller中的Delete方法,并且应该通过保持在同一页面上来删除记录。那么,下面的Delete方法不使用[HttpPost]是错误的吗?

2)如果可能的话,我应该怎么做才能将 [HttpPost] 用于 Delete 方法?我必须对我的视图进行哪些更改,即使用表单或隐藏属性?



看法:

@model IEnumerable<MyProject.Domain.Entities.Applicant>
@using PRMeetingReg.WebUI.HtmlHelpers

@{
    var grid = new System.Web.Helpers.WebGrid(
        source: Model,
        columnNames: new List<string>() { "Title" },
        ajaxUpdateContainerId: "myGrid",
        defaultSort: "Name",
        canPage: true,
        canSort: true,
        rowsPerPage: 5
        );
    grid.SortDirection = SortDirection.Ascending;
}

<div class="Grid">
    @grid.GetHtml(
          tableStyle: "table", 
          headerStyle: "webgrid-header",
          footerStyle: "webgrid-footer", 
          rowStyle: "webgrid-row-style",
          alternatingRowStyle: "webgrid-alternating-row",
          selectedRowStyle: "webgrid-selected-row",
   firstText: "<<",
   lastText: ">>",
   mode: WebGridPagerModes.All,
   fillEmptyRows: true,
   numericLinksCount: 5,

   columns: grid.Columns(
    grid.Column("ApplicantID", "No", canSort: true),
    grid.Column("Name", "Name", canSort: true),
    grid.Column("Surname", "Surname", canSort: true),

    //for using multiple Html.ActionLink in a column using Webgrid
    grid.Column("Actions", format: (item) =>
     new HtmlString(
          @Html.ActionImage("../../Content/icons/detail.png", "Detail", "icon-link", "Detail", "Admin", new { applicantId= item.ApplicantID }).ToString() +
          @Html.ActionImage("../../Content/icons/edit.png", "Edit", "icon-link", "Edit", "Admin", new { applicantId= item.ApplicantID }).ToString() +
          @Html.ActionImage("../../Content/icons/delete.png", "Delete", "icon-link", "Delete", "Admin", new { applicantId= item.ApplicantID }).ToString()
     )
    )
    )
    )


<p>
    <input id="add" type="submit" value="Yeni Ekle" class="button" />   
</p>

</div>



控制器:

[HttpPost]
public ActionResult Delete(int applicantId)
{
    Applicant deletedApplicant = repository.DeleteApplicant(applicantId);
    if (deletedApplicant != null)
    {
        TempData["message"] = string.Format("{0} was deleted",
        deletedApplicant.Name);
    }
    return RedirectToAction("Index");
}



我的 HTML 助手:

public static MvcHtmlString ActionImage(this HtmlHelper html, string imagePath, string alt, string cssClass,
       string action, string controllerName, object routeValues)
{
    var currentUrl = new UrlHelper(html.ViewContext.RequestContext);
    var imgTagBuilder = new TagBuilder("img"); 
    imgTagBuilder.MergeAttribute("src", currentUrl.Content(imagePath));
    imgTagBuilder.MergeAttribute("title", alt);
    imgTagBuilder.MergeAttribute("class", cssClass);
    string imgHtml = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
    var anchorTagBuilder = new TagBuilder("a"); 
    anchorTagBuilder.MergeAttribute("href", currentUrl.Action(action, controllerName, routeValues));
    anchorTagBuilder.InnerHtml = imgHtml; 
    string anchorHtml = anchorTagBuilder.ToString(TagRenderMode.Normal);
    return MvcHtmlString.Create(anchorHtml);
}

提前致谢。

4

1 回答 1

1

您的ActionImage自定义助手生成包含 ( <a>) 内的锚标记的图像。在 HTML 中,锚点发送 GET 请求。您的控制器操作被 HttpPost 请求修饰,这解释了为什么它从未被调用。

一种可行的方法是使用 AJAX 请求并在单击 Delete 链接时执行 POST 请求而不是 GET。

于 2013-11-06T10:09:47.393 回答