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.
Related
I want to use a string variable which could contain the values h1, h2, h3 etc to build some html. This works fine for the opening tag, but does not work nicely for the closing tag. If I write
#{ var tag = "h1" ; }
<#tag>some title here</#tag>
I end up with the html
<h1>some title here</#h1>
A work-around which seems to work is
<#tag>some title here<#("/"+tag)>
but it's pretty ugly. Is there some escape sequence I need to use here?
You can use Html.Raw.
string lineTemplate = "<h{0}>{1}</h{0}>";
for (int tagCounter = 1; tagCounter < 7; tagCounter++)
{
#Html.Raw(string.Format(lineTemplate, tagCounter, "Header "+ tagCounter));
}
i am not sure which Razor version you are using
but i tested your code in my MVC4, it works perfectly
it will render <h1>something</h1>
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';
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.
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>
}
I hope/suspect this is easy, so I will ask here and make a fool out of my self if it is.
I have a foreach loop in my view, mind you this is a Razor view. I dont know if the ASP.NET View engine does the same... but it might. I want to flip a bool on each loop, but it does not see to let me. The view engine chokes to death. Why? How can I fix it? I did a for loop and I did mod 2 for now, but I really need to understand this.
This is what I tried:
#{
var IsOdd = false;
}
#foreach(var foo in bar)
{
#{ IsOdd = !IsOdd; }
<div class="#(IsOdd ? "odd" : "even")">#foo</div>
}
Try this:
#{
var IsOdd = false;
}
#foreach(var foo in bar)
{
IsOdd = !IsOdd;
<div class="#(IsOdd ? "odd" : "even")">#foo</div>
}
(Worked for me with MVC 3 RC.)