有条件地显示的WebGrid图像 - MVC 3 [英] Conditionally display an image in webgrid - mvc 3

查看:125
本文介绍了有条件地显示的WebGrid图像 - MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WebGrid我需要基于以下

.. code被赋予值来显示图像

  @model TraktorumMVC.Models.ManagePhotos
@ {
    ViewBag.Title =ManagePhotos;
    布局=〜/查看/共享/ _Layout.cshtml
    VAR电网=新的WebGrid(Model.AdPhotos);
}
    @ grid.GetHtml(
       displayHeader:假的,
       列:grid.Columns(
             grid.Column(格式:(项目)=>
                 {
                     如果(item.IsMain preVIEW == true)而
                     {
                         返回@<文本>< IMG SRC =@ Url.Content(〜/内容/图片/ preVIEW-photo.gif),ALT =图片/>< /文取代;
                     }
                     其他
                     {
                         返回@<文本>< IMG SRC =@ Url.Content(〜/内容/图片/非preVIEW-photo.gif),ALT =图片/>< /文本> ;
                     }
                 }
                )
             grid.Column(格式:(项目)=> Html.ActionLink(删除照片,RemovePhoto,图像,新{PHOTOID = @ item.Id},{新@class =RemovePhoto}))
         ));

我不知道我怎样才能在使用的WebGrid 如果。我只是去尝试。其工作不.getting以下错误

 的最好重载方法匹配'System.Web.Helpers.WebGrid.Column(字符串,字符串,System.Func<动态,对象&gt ;,字符串,布尔)'有一些无效参数


解决方案

grid.Column 方法的格式参数你正在组建一个拉姆达前pression,让你当然可以用如果。但问题是你不能使用 @ 当你在code模式,在剃刀输出HTML。所以,你需要用图片标记创建成的HtmlHelper(如建于 Html.ActionLink 有很多的范例),或使用HTML.Raw方法返回HTML:

  @ grid.GetHtml(
    displayHeader:假的,
    列:grid.Columns(
            grid.Column(格式:(项目)=>
                {
                    如果(item.IsMain preVIEW == true)而
                    {
                        返回Html.Raw(的String.Format(<文本>< IMG SRC = \\{0} \\ALT = \\图像\\/>< /文本>中,Url.Content(〜/内容/图片/ preVIEW-photo.gif)));
                    }
                    其他
                    {
                        返回Html.Raw(的String.Format(<文本>< IMG SRC = \\{0} \\ALT = \\图像\\/>< /文本>中,Url.Content(〜/内容/图片/非preVIEW-photo.gif)));
                    }
                }
            )
            grid.Column(格式:(项目)=> Html.ActionLink(删除照片,RemovePhoto,图像,新的{PHOTOID = item.Id},{新@class =RemovePhoto}))
        ));

另外,在最后一行,而不是新{PHOTOID = @ item.Id} 你应该写新{PHOTOID = item.Id} 结果
要了解更多关于剃须刀这里是一个详细的<一个href=\"http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax\">tutorial.

In my webgrid I need to display images based on the value .. Code is given below

@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" }))
         ));

I am not sure how can i use if in webgrid . I just tried that .Its not working .getting following error

The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func<dynamic,object>, string, bool)' has some invalid arguments

解决方案

In thegrid.Column method's format parameter you are putting together a lambda expression so that you can of course use if. But the problem is you cannot use @ when you are in "code mode" in Razor to output HTML. So you need to wrap the image tag creation into an HtmlHelper (like the built in Html.ActionLink there are lots of examples) or use the HTML.Raw method to return 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")));                         
                    }
                }
            ),               
            grid.Column(format: (item) => Html.ActionLink("Remove Photo", "RemovePhoto", "Images", new { photoID = item.Id }, new { @class = "RemovePhoto" }))
        ));

Also in the last line instead of new { photoID = @item.Id } you should write new { photoID = item.Id }
To learn more about razor here is a detailed tutorial.

这篇关于有条件地显示的WebGrid图像 - MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆