Cannot convert from 'lambda expression' to 'system.func dynamic object ' MVC3 Razor

左心房为你撑大大i 提交于 2019-12-12 22:37:29

问题


I'm getting the mentioned error and "Invalid arguments" for WebGrid.column error in my view page.

//View

@{
    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>)       
     })    
}

The errors are thrown in the last column(ContactUsId) near the format:. Where i'm wrong?

Kindly help.


回答1:


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.

Example: Something like this

@{
    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)    
}

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).

As a readability improvement I would use string.Format to replace markers in the template string.

e.g.

    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);


来源:https://stackoverflow.com/questions/23366843/cannot-convert-from-lambda-expression-to-system-func-dynamic-object-mvc3-ra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!