我有一个强类型视图,我在一个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>
非常感谢任何帮助。
答案 0 :(得分: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' /> <# } #>");
答案 1 :(得分: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();
%>