Special characters after syntax - asp.net-mvc

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.

Related

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.)

Using Asp.net mvc razor, how to not have to use div tag to force html output

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/

Razor View display specific HTML tags

To make Razor View display HTML tags to browser we use this
#Html.Raw(Model.Message)
I want only b,img allowed to be displayed. ( Without write a new method to remove all other tags )
Is Razor support it, or method to remove all html tag except b and img ?
Razor doesn't parse html. It can encode or decode it, but if you need to remove some tags inside your Model.Message - you need to parse it before saving it or displaying.

line breaks in struts2 textarea

Is there way to add line breaks to <s:textarea> value?
<s:textarea name="text_area" value="need_some_line_breaks" />
thanks.
This isn't Struts 2-related at all, rather it's basic HTML.
If you want newlines inside a <textarea> put a newline ("\n") in the string wherever you want one. Personally, I leave textarea data untouched and transform it only when I need to view it outside a text area, by replacing the newlines with <br/> tags. This way the original data is always preserved.

mvc formatting text

i have text like this
div bla-bla end div
i need to get only 'bla-bla' without div, because of i need to call substring in controller only to text bla-bla not to div tags. is it possible
p.s. how to input tags here?
If you have jquery you can access inner html by calling ( for example ):
$("#idofthediv").html()
to set the conntent of the div:
$("#idofthediv").html('some html')
document.findElementById(..).innerText;
Take a look at using a Regular Expression.
There is a sample of what you want to do at Regular Expression Examples. It's the first sample in the section titled "Grabbing HTML Tags" near the top of the page.

Resources