-->

How to bind images in Telerik Grid for ASP.NET MVC

2019-03-05 11:30发布

问题:

Please let me know how to bind a image static image with all rows in Telerik Grid for ASP.NET MVC.

 <%= Html.Telerik().Grid(Model.SearchResponse)
               .Name("SearchGrid")
               .Columns(columns =>
                   {
                       //Here i need to bind a static image column//

                       columns.Bound(grid => grid.Name);
                       columns.Bound(grid => grid.CaseNumber);
                     })
                   .Pageable(true)
    %>

回答1:

This is possible by adding another templated column to your collection:

Using ASPX

columns.Template(c => { 
%><img alt="Static Image Alt Text" src="<%= Url.Content("~/myImage.jpg") %>" 
/><% 
}).Title("Static Image");

Using Razor

columns.Template(
   @<text>
     <img alt="Static Image Alt Text" src="@Url.Content("~/myImage.jpg") " />
  </text>
).Title("Static Image");

UPDATE: If you wish to bind images from your model, please refer to the following example:

columns.Template(c => {
%>
<img 
alt="<%= c.CustomerID %>" 
src="<%= Url.Content("~/" + c.CustomerID + ".jpg") %>" 
/>
<%
});

Or if you're using client templates, try the following:

.Columns(columns =>
{
columns.Bound(c => c.CustomerID)
.ClientTemplate("<img alt='<#= CustomerID #>' src='" 
+ Url.Content("~/") 
+ "<#= CustomerID #>.jpg' />")
.Title("Picture");
//omitted for brevity
}


回答2:

This way we can bind image with every row. we can also add Action with these images.

columns.Command(commands => commands.Custom("View").ButtonType(GridButtonType.BareImage)