Search code examples
htmlcssasp.net-mvc-4webgrid

CSS Style in webgrid MVC


I want to apply CSS style in following code but i couldnt find the way. I want to add style to image as width 50px and height 50px

grid.Column(format:(item) =>
    {
            if (File.Exists(Server.MapPath("~/Content/BrandImages/" + item.BrandImage)))
            {
                return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\"/></text>", @Url.Content("~/Content/BrandImages/" + item.BrandImage)));
            }
            else
            {
                return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\"/></text>", Url.Content("~/Content/BrandImages/noimage.jpg")));
            }
        }
    ),

Solution

  • You have multiple solutions

    1-You can add inline width and height properties to your img element

      return Html.Raw(string.Format("<text><img style=\"width:50px; height:50px;\" src=\"{0}\" alt=\"Image\"/></text>", @Url.Content("~/Content/BrandImages/" + item.BrandImage)));
    

    2- In your global stylesheet you can define new style like below

    .myImageClass{
        width:50;
        height:50px;
    }
    

    then you can add this class your image elements

    return Html.Raw(string.Format("<text><img class=\"myImageClass\" src=\"{0}\" alt=\"Image\"/></text>", Url.Content("~/Content/BrandImages/noimage.jpg")));