0

我有一个强类型视图,我在 Telerik 网格的一列中显示带有删除图像的顾问。如果顾问处于活动状态或处于非活动状态,我会在其中存储一个“状态”列。我想要做的是,如果顾问具有“活动”状态,则显示灰色删除图像(禁用,无法删除顾问,因为他/她处于活动状态),否则显示红色删除图像(启用,可以删除顾问因为他/她不活跃)。我在下面的设计可以做到这一点吗?

控制器动作:

public ActionResult Index()
{
    ViewData.Model = db.Consultants.OrderBy(p => p.ConsultantName);
    return View();
}

看法:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UI.Models.Consultants>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

                <table>
                    <tr>
                        <td><% Html.GridFor("Consultants", "Consultants", "Consultants", GridOptions.EnableSelecting, Model).Columns(column =>
                                {
                                    column.Bound(o => o.Code).Title("Code");
                                    column.Bound(o => o.Description).Title("Description");                                  
                                 column.Template(o =>
            {
                %>
                <img src="/Content/img/delete.png" alt="consultant" title="consultant" onclick="javascript:deleteConsultant(<%= o.consultantKey %>)" />
                <%
            }).Title("").ClientTemplate(
                      "<img src=\"/Content/img/delete.png\" alt=\"consultant\" title=\"consultant\" onclick=\"javascript:deleteConsultant(<#= ProjectKey #>)\"/>");
            column.Bound(o => o.consultantKey).Hidden();
        }).Render();            
                            %>
                        </td>
                    </tr>
                </table>
</asp:Content>

任何帮助深表感谢。

4

2 回答 2

2

是的,有可能。您可以在模板中包含条件逻辑。这是一个例子:

columns.Template(o => 
{
     if (o.Foo)
     {
       %> 
           <img src="img1.gif" />
       <%
     }
     else
     {
       %>
           <img src="img2.gif" />
       <%
     }
}).ClientTemplate("<# if (Foo) { #> <img src='img1.gif'/> <# } else { #> <img src='img2.gif' /> <# } #>");
于 2012-11-29T21:29:34.187 回答
0

可能是这样的:

<%
String clientTempate;

if(Model.Active.Equals("Active"){
  clientTemplate = "<img src=\"/Content/img/delete.png\" alt=\"consultant\" title=\"consultant\" onclick=\"javascript:deleteConsultant(<#= ProjectKey #>)\"/>"
}
else{
  clientTemplate = "<img src=\"/Content/img/grayedout-delete.png\" alt=\"consultant\" title=\"consultant\" />"

}
%>


<% Html.GridFor("Consultants", "Consultants", "Consultants", GridOptions.EnableSelecting, Model).Columns(column =>
                                        {
                                            column.Bound(o => o.Code).Title("Code");
                                            column.Bound(o => o.Description).Title("Description");                                  
                                         column.Bound(o => o.consultantKey).Title("").ClientTemplate(clientTemplate);
                    column.Bound(o => o.consultantKey).Hidden();
                }).Render();            
                                    %>
于 2012-11-29T22:14:54.330 回答