在部分Razor视图中,我有这个:
@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.RenderPartial("_orderLinesList", Model.Items); %>
然后我用它将orderId传递给控制器:
<script type="text/javascript">
$(function () {
$("tbody tr").click(function () {
var url = '<%=Url.Action("SelectOrderLine", "DeliveryOrderLines", new {id = "__id__"}) %>';
url = url.replace('__id__', $(this).attr('id'));
location.href = url;
})
.hover(function () { $(this).addClass('highlight'); }, function () { $(this).removeClass('highlight'); });
});
</script>
来自我的控制器的ActionResult
public ActionResult SelectOrderLine(int id)
{
return RedirectToAction("Index", "DeliveryOrderLine", new {id = id});
}
我没有收到任何身份证明。
在渲染的html代码中,我看到没有任何Id可以从Controller中的ActionResult中发送。
网格行不包含任何按钮,如编辑,删除...这不是预期的。
我想为每一行定义一个id,并在选择一行时使用此id,然后从我的Controller中将id发送到ActionResult方法。
请告诉我如何解决这个问题?
谢谢。
答案 0 :(得分:2)
您可以在设置网格时指定每列的格式:
grid.Column("InvoiceID", header: "Invoice",
format: @<text>@Html.ActionLink((string)item.InvoiceID.ToString(),
"Invoice", "Invoice", new { invoiceID = item.InvoiceID }, null)</text>),
这将生成带有ID的相应链接以传回您的控制器。
答案 1 :(得分:0)
我知道这是一个古老的问题,但我也一直在与此作斗争。
我通过从webgrid修改生成的HTML,然后将其返回到视图进行渲染来处理它。
将此视图添加到视图中,并传入lambda以从模型项返回identifer值
foreach
这是一个非正规化的&#34;版本显示概念,在我的生产代码中我移动了&#34; SetRowIds&#34;进入一个新的功能。
我现在正在使用HTML拦截来生成行编辑模板,页脚等 - 它运行良好!