在我的网格网格中,我需要根据值显示图像.代码如下
@model TraktorumMVC.Models.ManagePhotos @{ ViewBag.Title = "ManagePhotos"; Layout = "~/Views/Shared/_Layout.cshtml"; var grid = new WebGrid(Model.AdPhotos); } @grid.GetHtml( displayHeader: false,columns: grid.Columns( grid.Column(format: (item) => { if (item.IsMainPreview == true) { return @<text><img src="@Url.Content("~/Content/images/preview-photo.gif")" alt="Image "/></text>; } else { return @<text><img src="@Url.Content("~/Content/images/non-preview-photo.gif")" alt="Image "/></text>; } } ),grid.Column(format: (item) => Html.ActionLink("Remove Photo","RemovePhoto","Images",new { photoID = @item.Id },new { @class = "RemovePhoto" })) ));
我不知道如果在webgrid我如何使用.我刚刚尝试,它不工作.getting以下错误
The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string,string,System.Func<dynamic,object>,bool)' has some invalid arguments
解决方法
在thegrid.Column方法的格式参数中,您将放置一个lambda表达式,以便当然可以使用if表达式.但问题是您在Razor中的“代码模式”中不能使用@来输出HTML.所以你需要将图像标签创建包装成一个HtmlHelper(像内置的Html.ActionLink有很多
examples)或使用HTML.Raw方法来返回HTML:
@grid.GetHtml( displayHeader: false,columns: grid.Columns( grid.Column(format: (item) => { if (item.IsMainPreview == true) { return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\"/></text>",Url.Content("~/Content/images/preview-photo.gif"))); } else { return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\"/></text>",Url.Content("~/Content/images/non-preview-photo.gif"))); } } ),new { photoID = item.Id },new { @class = "RemovePhoto" })) ));
同样在最后一行,而不是新的{photoID = @ item.Id},您应该写新的{photoID = item.Id}
要了解更多关于剃须刀的详细tutorial.