双引号之间的剃刀code [英] Razor code between double quotes

查看:172
本文介绍了双引号之间的剃刀code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在剃刀查看发动机模板,我要做到以下几点:我想提出一些的 code 的一个HTML的双引号之间的属性的。麻烦的是,我要插入件code包含一些双引号本身。

 < A HREF =Url.Action(项目,新{ID = Model.Item.Id,页=页});> @网页< / A&GT ;

您可以很容易地看到事情如何将可怕的错误:-)我知道我可以计算一个变量的链接,然后使用它,但我宁愿不要:

  @ {VAR行动= Url.Action(问题,新{ID = Model.Question.Id,页=页}); }
&所述; A HREF =@动作> @页与所述; / A>


解决方案

您不需要逃避或使用剃刀东西。剃刀是足够聪明,知道什么时候引号是属性中,因为你逃脱HTML之外,当你分析它。

 < A HREF =@ Url.Action(项目,
       新{ID = Model.Item.Id,页=页})> @网页< / A>

这code将正常工作 - 只要确保你有 @ 符号在 Url.Action 调用,因为如果不这样做就无法正确解析,我注意到你没有它在你的问题。

编辑:删除;作为Url.Action不是一个声明。

In a Razor View Engine template, I want to do the following: I want to put some code between the double quotes of an html attribute. The trouble is that the piece of code I want to insert contains some double quotes itself.

<a href="Url.Action("Item", new { id = Model.Item.Id, page = page });">@page</a>

You can easily see how things turn horribly wrong :-) I know i can calculate the link in a variable and then use it, but I'd rather not:

@{ var action = Url.Action("Question", new { id = Model.Question.Id, page = page }); }                   
<a href="@action">@page</a>                                        

解决方案

You don't need to escape or anything using Razor. Razor is smart enough to know when quotes are within attributes because you're escaping outside of html when you parse it.

<a href="@Url.Action("Item", 
       new { id = Model.Item.Id, page = page })">@page</a>

That code will work fine - Just make sure you have the @ symbol in front of the Url.Action call because if you don't it won't be parsed properly and I notice you don't have it in your question.

Edit: removed ; as Url.Action is not a statement.

这篇关于双引号之间的剃刀code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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