无法从'拉姆达前pression'到'system.func动态对象“MVC3剃刀转换 [英] Cannot convert from 'lambda expression' to 'system.func dynamic object ' MVC3 Razor
问题描述
我得到WebGrid.column错误提到的错误和参数无效在我看来页面。
I'm getting the mentioned error and "Invalid arguments" for WebGrid.column error in my view page.
//查看
@{
WebGrid grid = new WebGrid(Model.LoadProductDetails());
@grid.GetHtml(
tableStyle: "grid",
fillEmptyRows: false,
headerStyle: "gvHeading",
alternatingRowStyle: "gvAlternateRow",
rowStyle: "gvRow",
footerStyle: "gvFooter",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("ProductId",header: "ID"),
grid.Column("ProductName",header: "Product"),
grid.Column("Price"),
grid.Column("Qunatity"),
grid.Column("ReorderLevel", header: "R.O.L."),
grid.Column("ContactusId", header: "Action", canSort:false, format: @<text> @Html.Raw("<img src='/img/edit.png' title='Edit' onclick='EditProduct("+ item.ProductId ")' />") @Html.Raw("<img src='/img/delete.png' title='Delete' onclick='DeleteProduct("+ item.ProductId +")' />") </text>)
})
}
该错误抛出的最后一列(ContactUsId)附近的格式:。在那里我错了?
请帮助。
推荐答案
这是我的意见:它看起来像你试图注入客户端HTML到服务器端函数调用(这反过来又产生客户端输出) 。这就是MVC的工作原理(code注入到网页HTML)相反。你需要让那些@&LT;>到C#字符串。然后,它可以被处理服务器端和所得GetHtml方法将注入的最终结果到输出。
From my comment: It looks like you are trying to inject client-side HTML into a server-side function call (which in turn generates client-side output). That is the opposite of how MVC works (code injects into the page HTML). You need to make those @<> into C# strings. Then it can be processed server-side and the resulting GetHtml method will inject the final result into the output.
@{
string template = "<text><img src='/img/edit.png' title='Edit' onclick='EditProduct("+ item.ProductId + ")' /><img src='/img/delete.png' title='Delete' onclick='DeleteProduct("+ item.ProductId +")' /></text>";
WebGrid grid = new WebGrid(Model.LoadProductDetails());
@grid.GetHtml(
tableStyle: "grid",
fillEmptyRows: false,
headerStyle: "gvHeading",
alternatingRowStyle: "gvAlternateRow",
rowStyle: "gvRow",
footerStyle: "gvFooter",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("ProductId",header: "ID"),
grid.Column("ProductName",header: "Product"),
grid.Column("Price"),
grid.Column("Qunatity"),
grid.Column("ReorderLevel", header: "R.O.L."),
grid.Column("ContactusId", header: "Action", canSort:false, format: template)
}
道歉任何错别字,但是这仅仅是指示你应该做的事情,而不是试图用剃刀语法(这永远不会工作)注入HTML到C#。
Apologies for any typos, but this is just to indicate what you should be doing instead of trying to inject HTML into C# using Razor syntax (which would never work).
作为一个可读性提高,我会用的String.Format
模板中的字符串替换标记。
As a readability improvement I would use string.Format
to replace markers in the template string.
例如
string template = string.format("<text><img src='/img/edit.png' title='Edit' onclick='EditProduct({0})' /><img src='/img/delete.png' title='Delete' onclick='DeleteProduct({0})' /></text>", item.ProductId);
这篇关于无法从'拉姆达前pression'到'system.func动态对象“MVC3剃刀转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!