RAZOR and prepending text in href - asp.net-mvc

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

Related

Refer fragment in href or attribute for thymleaf template

is it possible to use a fragment value inside an html attribute like href?
im trying something
<a th:href="~{text:test::text-login}">Tada</a>`
but url comes out like
LOGIN</th:block>">Tada
Im expecting
Tada
I tried out some other syntaxes but got same result
<a th:href="#{{logourl}(logourl=~{text:test::text-login})}">Eureka</a>
You can get it to do exactly what you want by adding /text() to your fragment expression:
<a th:href="~{text:test::text-login/text()}">Tada</a>

Razor, shortest conditional block

I frequently use code like:
<p #if(Model.Sth)
{
?:style="display: none;"
}>Some text</p>
Many template engines have special markers for conditional blocks, for example in Mustache you can write:
<p {{#Model.Sth}}style="display: none;"{{/Model.Sth}}>Some text</p>
Can my Razor code can be written in shorter form?
In Razor you can embed expressions inside parenthesis to execute them inline. If your conditional can be written using the ternary operator, then you can do something like this:
#(this.Model.Sth ? "style='display:none;'" : string.Empty)
The trick is getting Razor to emit the resulting string correctly back into your HTML. You could use HtmlHelper to do it, but it gets messy enough that the long-form conditional is much cleaner.
In the specific case of an attribute, however, there's a special feature of Razor, as of MVC4, that will help. If you specify an attribute using an expression that evaluates to null, MVC won't emit the attribute at all, so you can do:
<p style="#(this.Model.Sth ? "display:none;" : null)">Some Text</p>
(Note that null and string.Empty are different in this case: Razor will emit style="" if your expression evaluates to empty string.)

How do I escape curly braces in Asp .Net MVC Razor?

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!

Special characters after syntax

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.

Why is the string that I constructed rendered as escaped HTML?

I want to add a link to each element of an array, then join the resulting array. I tried:
myarray.collect{|u| link_to u[:first_name], username_path(u[:username])}.join(', ')
This does everything correctly, except it returns:
<a href="/niels">Niels Bohr</a>, <a href="/richard">Richard Feynman</a>
Instead of
Niels Bohr, Richard Feynman
How do I fix this? Or is there a simpler way of proceeding?
Thanks.
Use html_safe
myarray.collect{|u| link_to u[:first_name], username_path(u[:username])}.join(', ').html_safe
There is nothing wrong with adding the links or joining the elements of the list. That all works fine. What is wrong is that your string is considered unsafe and some of the characters used to construct valid HTML (and more importantly, javascript) are being escaped.
As fl00r says, you should add
.html_safe after the string, to tell the rendering function that any HTML in the function can be safely sent to the browser as-is.

Resources