【发布时间】:2013-11-06 09:58:47
【问题描述】:
当我在下面使用 [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);
}
提前致谢。
【问题讨论】:
-
你的视图应该有一个表单?
-
其实没有,但是为了使用相关id的隐藏属性并执行POST,我认为我需要使用Form元素。
标签: asp.net-mvc gridview webforms http-post http-get