Asp.net mvc 无法使用MVC4中的[HttpPost]进行删除
当我在下面用[HttpPost]定义Delete方法时,无法从视图中调用Delete方法。但是,当删除[HttpPost]行时,它工作正常。我尝试了很多东西,实际上这可能和我认为@Html.Hidden或@使用(Html.BeginForm()的错误用法有关。所以,你们能在下面澄清我的观点吗 1) 单击WebGrid上的“删除”按钮后,我不会打开视图。在一个确认方法之后,应该调用控制器中的Delete方法,并通过保持在同一页面上来删除记录。那么,我下面的Delete方法不使用[HttpPost]有错吗 2) 如果可能,我应该如何使用[HttpPost]来删除方法?我必须对视图进行哪些更改,即使用表单或隐藏属性Asp.net mvc 无法使用MVC4中的[HttpPost]进行删除,asp.net-mvc,gridview,webforms,http-post,http-get,Asp.net Mvc,Gridview,Webforms,Http Post,Http Get,当我在下面用[HttpPost]定义Delete方法时,无法从视图中调用Delete方法。但是,当删除[HttpPost]行时,它工作正常。我尝试了很多东西,实际上这可能和我认为@Html.Hidden或@使用(Html.BeginForm()的错误用法有关。所以,你们能在下面澄清我的观点吗 1) 单击WebGrid上的“删除”按钮后,我不会打开视图。在一个确认方法之后,应该调用控制器中的Delete方法,并通过保持在同一页面上来删除记录。那么,我下面的Delete方法不使用[HttpPost]
查看:
@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");
}
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);
}
我的HTML助手:
@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");
}
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);
}
提前感谢。您的
ActionImage
自定义帮助程序将生成一个包含锚定标记的图像(
)。在HTML中,锚发送GET请求。您的控制器操作被HttpPost请求修饰,这解释了为什么从未调用它
一种可行的方法是使用AJAX请求并执行POST请求,而不是在单击删除链接时执行GET。您的视图应该有一个表单?实际上没有,但为了使用相关id的隐藏属性并执行POST,我认为我需要使用表单元素。非常感谢您的良好解释。在这种情况下,导致这种情况的因素是锚元素。在这种情况下,我认为在删除记录时使用GET不是一个好主意。这是真的吗?另一方面,你能给我一个好的样品来删除一个记录吗?对不起,我以前从未使用过这种方法,我需要根据我的视图调整这种方法(如果必要,我应该修改控制器的Delete方法)。再次感谢…是的,GET不是在服务器上删除某些内容的正确动词。如果你将这个链接AJAXify,你甚至可以在这种情况下使用最正确的动词DELETE。我如何将它AJAXify?你能举个例子吗?提前感谢。您可以使用
jQuery.ajax
函数。