我一直在將我的網格從標準表格改寫爲一個支持分頁和排序的webgrid,但是我在添加「刪除」列時遇到了問題。這是我對刪除原代碼:WebGrid列中的ASP.NET MVC Html.BeginForm
@using (Html.BeginForm("Delete", "Admin")
{
@Html.Hidden("ProductId", item.ProductId)
<input type="submit" class="btn btn-default btn-xs" value="Delete" />
}
我無法理解清楚如何重寫所以它的工作原理和以前一樣的的WebGrid。這是我的網格到目前爲止:
@grid.Table(
tableStyle: "table table-striped table-condensed table-bordered",
columns: grid.Columns(
grid.Column(columnName:"ProductId", header: "Id"),
grid.Column(columnName: "Name", header: "Name", format: (item) =>
{
var link = Html.ActionLink((string)item.Name, "Edit", new { item.ProductId });
return link;
}),
grid.Column(columnName: "Price", header: "Price"),
grid.Column(here should be the delete button)
)
)
你能幫我一下嗎?提前致謝!
編輯:我已經試過是:
grid.Column(format: (item) =>
{
using (Html.BeginForm("Delete", "Admin"))
{
string htmlString = string.Empty;
Html.Hidden("ProductId", (string)item.ProductId);
htmlString = "<input type = \"submit\" class=\"btn btn-default btn-xs\" value=\"Delete\" />";
return new HtmlString(htmlString);
}
})
但它似乎並沒有工作。
編輯2:這裏是工作刪除,可悲沒有剃鬚刀。
@using (Html.BeginForm("Delete", "Admin"))
{
@grid.Table(
tableStyle: "table table-striped table-condensed table-bordered",
columns: grid.Columns(
grid.Column(columnName: "ProductId", header: "Id"),
grid.Column(columnName: "Name", header: "Name", format: (item) =>
{
var link = Html.ActionLink((string)item.Name, "Edit", new { item.ProductId });
return link;
}),
grid.Column(columnName: "Price", header: "Price"),
grid.Column(columnName: "", header: "", format: @<text> <input id="ProductId", name="ProductId", type="hidden", value="@item.ProductId" /><input type="submit" class="btn btn-default btn-xs" value="Delete" /></text>)
)
)
}
不能你做同樣的你的編輯?只是將操作鏈接更改爲'你試過@ Html.ActionLink(「刪除」,「刪除」,「管理員」,新的{id = item.ProductId})''嗎? –
我總是喜歡用手寫HTML來呈現表格數據,如果我需要分頁,請使用https://github.com/dncuug/X.PagedList。這使我完全控制了我爲表格數據呈現的代碼。 – Shyju