Strange error in Razor view, in Orchard CMS - asp.net-mvc

When I have this:
#using Orchard.Themes.Models
#using Orchard.Themes.Preview
#using Orchard.Themes.Services
#using Orchard.Themes.ViewModels
#{
Script.Require("OrchardTinyMceDeluxe");
var pluginsBaseUrl = #Url.Content("~/modules/tinymcedeluxe/scripts/plugins");
var siteThemeService = WorkContext.Resolve<ISiteThemeService>();
}
I get this error:
Parser Error Message: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
Line 4: #using Orchard.Themes.Services
Line 5: #using Orchard.Themes.ViewModels
Line 6: #{
Line 7: Script.Require("OrchardTinyMceDeluxe");
Line 8: var pluginsBaseUrl = #Url.Content("~/modules/tinymcedeluxe/scripts/plugins");
But if I break the code up into two separate C# blocks, as shown below, it works fine. Why?
#{
Script.Require("OrchardTinyMceDeluxe");
var pluginsBaseUrl = #Url.Content("~/modules/tinymcedeluxe/scripts/plugins");
}
#{
var siteThemeService = WorkContext.Resolve<ISiteThemeService>();
}

You shouldn't use the # on Url.Content, it's already inside a code block.
What I think is happening is that razor is getting confused by the # and the semicolon at the end, thus placing the close bracket into the HTML.

Related

#: in a foreach loop doesn't work when it's between tags?

I know that #: is for output a single line of content containing plain text or unmatched HTML tags;
If so , why do I get an error for :
( ":" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.)
#foreach (int line in new int[] { 0, 1 })
{
<span>
#: aaaaa
</span>
}
While this run as expected :
#foreach (int line in new int[] { 0, 1 })
{
#: aaaaa
}
#: is used to mix text within a code block.
When you use the span tag or any other html elements, inside a C# code block, you are already mixing HTML tag and razor can properly parse and execute it. When you use #: inside an HTML element (non-code block), it is not even valid.
In short, you can use HTML elements inside a code block as wrapper to mix plaintext/html.
#foreach (int line in new int[] { 0, 1 })
{
<div>Hello</div>
}
In razor, the #: tag produce the same result as the text tag when it comes to mixing plain text within a code block. The below will also produce same result as your second code block.
#foreach (int line in new int[] { 0, 1 })
{
<text>aaa533</text>
}

asp.net mvc razor string with double quotes

I am trying to use a string with double quotes but unable to get it work
#{
string disableMessage = "";
var disableAttr = "";
if (ViewBag.IsApplicable)
{
disableMessage = "You dont have permission to add new Item.";
disableAttr = "class=" + "disableItem" +" title="+"\""+ disableMessage +"\"";
}
}
expected: disableAttr as
class=disableItem title="You dont have permission to add new demand."
I got struck at getting double quotes for title attribute.
Why not deal with the two attributes separately:
#{
string disableTitle = null;
string disableClass = null;
if (ViewBag.IsApplicable)
{
disableTitle = "You dont have permission to add new Item.";
disableClass = "disableItem";
}
}
<div class="#disableClass" title="#disableTitle">Content</div>
Note that Razor V2 (in MVC4+) has a "conditional attribute" feature. When an attribute value is null, then Razor won't output anything at all for the attribute. So in the example above, if ViewBag.IsApplicable is false, the output will be:
<div>Content</div>
Ross's answer is much more elegant. However, keeping the line of your original code, you could do the following:
disableAttr = "class='disableItem'" +" title='"+ disableMessage +"'";
This will render the following text inside disableAttr:
class='disableItem' title='your_message_here';

Warning on Razor when <img width="#someVar">

Having the following .cshtml :
#{ int someVar = 300; }
<img width="#someVar">
Visual Studio (2012) gives me a warning :
Warning 1 '#width' is not a valid value of attribute 'width'.
It does work ok, but I'm wondering if there's any way to pass a variable to the width attribute without having this warning.
Thanks!
Just wrap the value in parentheses, like so:
#{ int someVar = 300; }
<img width="#(someVar)">
The warning should then disappear.

How do I use ViewBag string in partial view javascript

I am returning this from a partial view:
<script>
document.getElementById("login").style.visibility = "hidden";
document.getElementById("displayname").innerHTML = #ViewBag.DisplayName
</script>
The second script line, however, does not work.
How to write this correctly?
Greg
#Html.Raw("document.getElementById(\"displayname\").innerHTML = " + #ViewBag.DisplayName)

asp.net mvc razor multiply two item and convert to string

When I write #(line.Quantity * line.Product.Price).ToString("c") the result is
39,00.ToString("c")
and #line.Quantity * line.Product.Price.ToString("c") result is
2 * line.Product.Price.ToString("c")
How can i multiply two values and convert it to string in a razor view?
try
#((line.Quantity * line.Product.Price).ToString("c"))
The problem is that razor do not know when the output string ends since # is used to display code in HTML. Spaces switches razor back to HTML mode.
Wrapping everything into parenthesis makes razor evaluate the entire code block.
Although the most proper way would be to introduce a new property in your model:
public class MyModel
{
public double Total { get { return Quantity * Product.Price; }}
//all other code here
}
and simply use:
#line.Total.ToString("c")
this is an old question but I have just had the same issue and here is the resolution for it.
If you need to perform a calculation on a razor view, you can do it the following way:
if you are outside of c# block (such as #foreach or #if ):
you can wrap your calculation into #{ } and they won't be rendered.
<p>Some text</p>
#{ var x = Model.Y * Model.Z; }
<p>X equals #x.ToString()</p>
if you are inside of a c# block:
you can simply put your calculations in { }.
<p>Some text</p>
#foreach (var x in Model.Y)
{
{ var z = x * 2; }
<p>Z equals #z.ToString()</p>
}

Resources