I'm trying to bind a data attribute as follows:
#Html.Label("test", new { data_test = "{{vm.test}}" })
When rendered, what I see is:
<label data-test for="test">test</label>
How do I escape the curly braces so that they're rendered on the page? Some things a I've tried are:
{{{{vm.test}}}}
\\{\\{vm.test\\}\\}
If you need to display the curly braces, as when you're explaining handlebar logic and you don't need any other constructs:
Code:
<note>Example: #("{{object.exampleField}}")</note>
Displayed:
Example: {{object.exampleField}}
Inside a code block, you cannot use # characters to create more code blocks.
something like:
#Html.Label("test", new { data_test = "{{#vm.test}}" })
To write curly braces "{}" in razor use the combination of "at" and "colon".
Example: #:{
I had just figured it out while using handle bars.
And to write "#" use one more "at" sign.
Example: ##
I hope it helps the new ones who are looking for it!
Related
I've got the following cshtml code in my razor view.
<div class="col left-col pull-left">
#for (int i = 0; i < #Model.NewsResults.Count; i = i + 2)
{
NewsResult article1 = #Model.NewsResults[i];
<div>#i - #article1.Title #Html.Partial("NewsItemPartial", article1)</div>
}
</div>
The #i ... seems to tell razor to ouput the data. If I leave the out there, nothing renders.
Please explain how I can do what I have below but without having to use the (before the #i).
You can tell razor to output HTML by putting the text tag instead of div:
<text>#i - #article1.Title #Html.Partial("NewsItemPartial", article1)</text>
Razor is getting confused because it thinks the - is a subtraction operator, and it's trying to subtract Title from i. you could use Mark's method, or you could make it even more localized by putting the <text> block around the -.
#i <text>-</text> #article1.Title #Html.Partial("NewsItemPartial", article1)
However, it looks to me like you're trying to implement a template output, which MVC already provides a mechanism for, called DisplayTemplates. You should really use those instead of Partials.
As has already been mentioned, Razor is considering the - to be a subtraction operator because of its position right after the #i, which is converting it into a C# block.
You can use <text></text> to convert any block (single line or multiline) into a text literal in Razor.
#i <text>-</text> #article1.Title #Html.Partial("NewsItemPartial", article1)
Or better yet, for a single line (or single character as your example), you can alternatively use the #: operator.
#i #:- #article1.Title #Html.Partial("NewsItemPartial", article1)
Reference: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/
I want to add a forward slash to one of my db seeds. Here is how I'm trying it:
Template.create! code: '<div style="background-image: url("/assets/forest-trees-hiker-hiking.jpg");></div>'
This results in spaces instead of slashes though...
style="background-image: url(" assets forest-trees-hiker-hiking.jpg");
It looks like you're very close with the suggestions noted in the comments, but you're missing one closing quotation mark after the semicolon:
"<div style='background-image: url(\"/assets/forest-trees-hiker-hiking.jpg\");'></div>"
Note that the entire div is wrapped in double quotes, and the style is wrapped in single quotes, which can be nested without confusion. But since you need to nest another string within those strings, you must escape the quotes around the url, in order to tell the program that you are not closing one of your existing quotation marks.
As #usmanali wrote, you have to use backslash escape sign \
This should works for you: Template.create! code: '<div style="background-image: url("\/assets\/forest-trees-hiker-hiking.jpg");></div>'
More reading: http://en.wikibooks.org/wiki/Ruby_Programming/Strings#Single_quotes
I would like to know how to and is it posible to add special charaters after Razor syntax.
Example:
#Html.Raw(Model.Text)()
The problem is with () at the end. I'd like to add it just after generated content.
As an alternative to the <text></text> markup, you can simply wrap your Html.Raw statement in parenthesis. Example:
#(Html.Raw(Model.Text))()
This will prevent razor from trying to parse the extra set of parenthesis as they help razor determine when to stop parsing the markup.
Try this:
#Html.Raw(Model.Text)<text>()</text>
<text></text> is special razor markup, not to be confused with actual HTML.
The <text> tag is an element that is treated specially by Razor. It causes Razor to interpret the inner contents of the <text> block as content, and to not render the containing <text> tag element (meaning only the inner contents of the <text> element will be rendered – the tag itself will not). This makes it convenient when you want to render multi-line content blocks that are not wrapped by an HTML element.
Quoted from ASP.NET MVC 3: Razor’s #: and <text> syntax.
I'm basically trying to produce jQuery effects based on the data I am coming in with from the server. I've tried multiple methods but its not coming out correctly
:javascript
"#{if #user.nil?}"
$('#test-container').show();
"#{end}"
The contents of #{...} need to be a single expression, and is allowed to go over multiple lines. Inside #{...} you can use literal strings simply by quoting them, and you don’t need quotes around the whole thing.
:javascript
#{if #user.nil?
"$('#test-container').show();"
end}
In this case you can do it as a single line:
:javascript
#{"$('#test-container').show();" if #user.nil?}
In general you want to avoid complex multiline interpolated blocks like in the first example. If necessary you should look at creating helpers to keep your views simple and understandable.
Does this work?
- unless #user
:javascript
$('#test-container').show();
I'm learning RAZOR.
I need to make an href tag unique, by adding a letter to the start of the #ref:
eg.
<a href="#p23">
In Razor, to populate the href tag from my model, I have:
<a href="#p#item.ID">
However, Razor doesn't recognise #item.ID, unless it has no characters in front of it.
<a href="#p #item.ID">
But that then invalidates the href.
Is there a way for me to add the letter 'p' to this, and still allow RAZOR to add the ID of the item?
Thank you,
Mark
Just wrap it in parenthesis to aid the parser:
<a href="#p#(item.ID)">
In VB enclose the variable with brackets: "#p#(item.ID)"
in c# it's either the same or use curly brackets.
fixit
just put brackets around the item, i.e.
<a href="#p#(item.ID)">
you can do this across all of the razor syntax where required to mix/match