Asp.net MVC4如何为WebGrid设置行id
从局部角度来看,我有以下几点:Asp.net MVC4如何为WebGrid设置行id,asp.net,asp.net-mvc-3,asp.net-mvc-4,c#-5.0,Asp.net,Asp.net Mvc 3,Asp.net Mvc 4,C# 5.0,从局部角度来看,我有以下几点: @model IList<Stratent.ScanCloud.Shared.DeliveryOrderLineDto> <div id="gridDiv" style="width:100%; max-height:250px; overflow:auto; border:1px solid #d1d3d4"> @{ var grid = new WebGrid(Model, defaultSort: "Da
@model IList<Stratent.ScanCloud.Shared.DeliveryOrderLineDto>
<div id="gridDiv" style="width:100%; max-height:250px; overflow:auto; border:1px solid #d1d3d4">
@{
var grid = new WebGrid(Model, defaultSort: "Date",
selectionFieldName: "SelectedRow",
`enter code here` fieldNamePrefix: "gridItem", ajaxUpdateContainerId: "grid");
}
@if (Model.Count > 0)
{
@grid.GetHtml(
tableStyle: "grid",
alternatingRowStyle: "gridrow_alternate",
//format:@<tr id= '@item.OrderId'> </tr>,
selectedRowStyle: "highlight",
rowStyle: "gridrow",
htmlAttributes: new {id = "grid"},
columns: grid.Columns(
grid.Column("OrderId", header: "OrderId " + Html.SortDirection(ref grid, "Client"), style: "width:9%"),
grid.Column("Client", header: "Client " + Html.SortDirection(ref grid, "Client"), style: "width:9%"),
grid.Column("Date", header: "Date " + Html.SortDirection(ref grid, "Date"), style: "width:9%"),
grid.Column("Time", header: "Time " + Html.SortDirection(ref grid, "Time"), style: "width:9%"),
grid.Column("Reference", header: "Reference " + Html.SortDirection(ref grid, "Reference"), style: "width:9%"),
grid.Column("Order", header: "Order " + Html.SortDirection(ref grid, "Order"), style: "width:15%"),
grid.Column("Customer", header: "Customer " + Html.SortDirection(ref grid, "Customer"), style: "width:15%"),
grid.Column("Street", header: "Street " + Html.SortDirection(ref grid, "Street"), style: "width:9%"),
grid.Column("Number", header: "No " + Html.SortDirection(ref grid, "Number"), style: "width:9%"),
grid.Column("Town", header: "Town " + Html.SortDirection(ref grid, "Town"), style: "width:9%"),
grid.Column("Crs", header: "Crs " + Html.SortDirection(ref grid, "Crs"), style: "width:15%"),
grid.Column("Document", header: "Document " + Html.SortDirection(ref grid, "Document"), style: "width:15%")
)
)
}
@if(Model.Count == 0)
{
<p>There are no OrderLines available.</p>
}
</div>
我没有收到任何身份证
在呈现的html代码中,我看到没有任何Id可从控制器发送到ActionResult
网格行不包含任何按钮,如编辑、删除。。。这不是故意的
我想为每一行定义一个id,并在选择一行时使用该id,然后从我的控制器将该id发送到ActionResult方法中
你能告诉我如何解决这个问题吗
谢谢。您可以在设置网格时指定每列的格式:
grid.Column("InvoiceID", header: "Invoice",
format: @<text>@Html.ActionLink((string)item.InvoiceID.ToString(),
"Invoice", "Invoice", new { invoiceID = item.InvoiceID }, null)</text>),
grid.Column(“发票ID”,标题:“发票”,
格式:@@Html.ActionLink((字符串)项.InvoiceID.ToString(),
“发票”,“发票”,新的{invoiceID=item.invoiceID},null)),
这将生成带有ID的适当链接,并将其传递回控制器。我知道这是一个老问题,但我也一直在努力解决这个问题 我通过修改webgrid生成的HTML来处理它,然后将其返回视图进行渲染
using HtmlAgilityPack;
public class SmartGrid<T> : WebGrid
{
...omitted for brevity...
public IHtmlString GetHtml(string tableStyle = null, ...omitted for brevity..., Func<T, object> getRowId = null)
{
IHtmlString html = base.GetHtml(tableStyle: tableStyle, ...omitted for brevity...);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html.ToString());
for (int i = 0; i < this.Rows.Count; i++)
{
HtmlNode node = _doc.DocumentNode.SelectSingleNode(String.Format("//table/tbody/tr[{0}]", i + 1));
node.SetAttributeValue("id", getRowId(this.Rows[i].Value));
}
return new HtmlString(doc.DocumentNode.OuterHtml);
}
使用HtmlAgilityPack;
公共类SmartGrid:WebGrid
{
…为简洁起见省略。。。
公共IHtmlString GetHtml(字符串tableStyle=null,…为简洁起见省略…,Func getRowId=null)
{
IHtmlString html=base.GetHtml(tableStyle:tableStyle,…为简洁起见省略…);
HtmlDocument doc=新的HtmlDocument();
doc.LoadHtml(html.ToString());
for(int i=0;i
public ActionResult SelectOrderLine(int id)
{
return RedirectToAction("Index", "DeliveryOrderLine", new {id = id});
}
grid.Column("InvoiceID", header: "Invoice",
format: @<text>@Html.ActionLink((string)item.InvoiceID.ToString(),
"Invoice", "Invoice", new { invoiceID = item.InvoiceID }, null)</text>),
using HtmlAgilityPack;
public class SmartGrid<T> : WebGrid
{
...omitted for brevity...
public IHtmlString GetHtml(string tableStyle = null, ...omitted for brevity..., Func<T, object> getRowId = null)
{
IHtmlString html = base.GetHtml(tableStyle: tableStyle, ...omitted for brevity...);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html.ToString());
for (int i = 0; i < this.Rows.Count; i++)
{
HtmlNode node = _doc.DocumentNode.SelectSingleNode(String.Format("//table/tbody/tr[{0}]", i + 1));
node.SetAttributeValue("id", getRowId(this.Rows[i].Value));
}
return new HtmlString(doc.DocumentNode.OuterHtml);
}