如何使用 ? :如果用剃刀语句和内联code块 [英] How to use ? : if statements with Razor and inline code blocks

查看:106
本文介绍了如何使用 ? :如果用剃刀语句和内联code块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新与新Razore视图引擎我的旧的.aspx的看法。我有一堆的我哪里有code这样的地方:

I'm updating my old .aspx views with the new Razore view engine. I have a bunch of places where I have code like this:

<span class="vote-up<%= puzzle.UserVote == VoteType.Up ? "-selected" : "" %>">Vote Up</span>

在理想情况下,我想做到这一点:

Ideally I'd like to do this:

<span class="vote-up@{puzzle.UserVote == VoteType.Up ? "-selected" : ""}">Vote Up</span>

然而,有这里的两个问题:

However there's two problems here:


  1. 投票了@ {puzzle.UserVote ....不治疗@符号为code块的起始

  2. @ puzzle.UserVote == VoteType.Up 看第一部分 @ puzzle.UserVote ,如果是应该使变量的值。

  1. vote-up@{puzzle.UserVote .... is not treating the @ symbol as a start of a code block
  2. @puzzle.UserVote == VoteType.Up looks at the first part @puzzle.UserVote as if it's supposed to render the value of the variable.

任何人都知道如何解决这些问题?

Anyone know how to address these issues?

推荐答案

这应该工作:

<span class="vote-up@(puzzle.UserVote == VoteType.Up ? "-selected" : "")">Vote Up</span>

这篇关于如何使用 ? :如果用剃刀语句和内联code块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆