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

问题描述

我正在使用新的 Razore 视图引擎更新旧的 .aspx 视图.我有很多地方有这样的代码:

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>

但是这里有两个问题:

  1. vote-up@{puzzle.UserVote .... 不会将@ 符号视为代码块的开始
  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>

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

10-29 10:11