当我使用[HttpPost]定义下面的Delete方法时,无法从View调用Delete方法。但是,当删除[HttpPost]行时,它正常工作。我尝试了很多东西,实际上它可能与我在视图中错误使用 @ Html.Hidden 或 @using(Html.BeginForm()有关。所以,可以请你澄清下面的这些要点吗?
1)单击我的WebGrid上的删除按钮后,我不打开View。在确认方法之后,应该调用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);
}
提前致谢。
答案 0 :(得分:1)
您的ActionImage
自定义助手会在其中生成包含锚标记的图像(<a>
)。在HTML中,锚点发送GET请求。您的控制器操作使用HttpPost请求进行修饰,该请求解释了从未调用它的原因。
使这项工作的一种可能性是在点击删除链接时使用AJAX请求并执行POST请求而不是GET。