如何通过控制器将类添加到html元素?

时间:2016-06-23 14:49:35

标签: c# html asp.net-mvc controller

我忙于mvc 5申请。我有一个数据库名称列表,以html显示。我使用html.actionlink按字母顺序过滤名称A, B, C, D ...Z。通过每个html.actionlink,我将每个字母作为Id参数传递,然后在我的控制器中,我通过查找.ToList()我的名字的第一个字母来过滤使用.Where()返回的名称匹配Id参数。

这一切都运作良好。我现在需要做的是,如果没有以某个字母开头的名字,那么该字母必须在视图中显示为灰色。

如何通过控制器将类添加到html元素?我需要确保如果没有某个字母的名字,那么我的html链接必须包含color: grey的css类。我不知道会有哪些名称,因为数据库是由管理员填充的。

3 个答案:

答案 0 :(得分:1)

您可以定义CSS类并在html助手中应用您的类。像这样:

.yourClassName
{
 color:grey;
}

申请你的班级:

@Html.ActionLink("Name", "{Controller}", null,new { @class ="yourClassName" })

答案 1 :(得分:0)

使用ActionLink函数,您可以传递一个匿名对象,其中的条目对应于Html属性。

查看对象类型的参数,在ActionLink函数中称为htmlAttributes。

这是一个例子(请注意,该类以@为前缀,因为它是一个关键字):

@Html.ActionLink(
       "Portfolio", 
       "Index", 
       "Portfolio", 
       routeValues: null, 
       htmlAttributes: new { @class = "grayed" }
)

答案 2 :(得分:0)

我不太确定我在你的问题中遵循设计流程,但我认为这段代码可以帮助你。

 ggplot(aes(x = a, y = b, group = c), data = df, na.rm = TRUE) + 
  geom_point(aes(color = GG, size = factor(c)), alpha=0.3) +
  scale_x_continuous(limits = c(-200,2300))+
  scale_y_continuous(limits = c(-1.8,1.5))+
  geom_hline(yintercept=0, size=0.4, color="black") +
  scale_color_distiller(palette="YlGnBu", na.value="white") +
  geom_smooth(method = "lm", aes(group = factor(GG), color = GG), se = F) +
  geom_label_repel(data = labelInfo, aes(x= max, y = predAtMax, label = label, color = label))

基本上,您可以使用Razor语法编写IF语句,然后通过执行Count检查传入数据是否为空,然后针对每种情况对元素设置不同的样式。

因为我不知道您为模型使用的名称,您应用于非灰色元素,控制器名称,操作名称的类,然后您需要编辑此代码才能使其工作