VS2013 Razor and Format Document - razor

Formatting this part of code via Ctrl+E, D:
if (row % 3 == 0)
{
#:</div>
}
gives me:
if (row % 3 == 0)
{
#:
</div>
}
which makes my .cshtml document invalid.
Any suggestions on how to prevent this in VS2013, but that my other code still gets proper formatting using Ctrl+E, D?

I get the same issue as you. The only way I managed to stop it was by rewriting it like this (you won't need the initial # before the if as long as you are already in a code block, but I did when I pasted it into my page to test it):
#if (row % 3 == 0)
{
#Html.Raw("</div>")
}
I first learnt about using #: as a replacement for when #Html.Raw didn't work.

Related

Razor page, server code with html

I can't figure out how to mix html with server code in this scenario.
#{
var i = 0;
foreach (var match in Model.StagingRooms)
{
if (i % 2 == 0)
{
<div class="row">
}
Html.Partial("_MatchCard", match.Value);
i++;
if (i % 2 == 0)
{
</div>
}
}
}
Using the code above, instead of rows of cards, I get an output of my code.
If I add # to Html.Partial and the increment
I also tried to append # to each server code line, and removing the #{} block, however this doesn't let me compile at all. I get a bunch of red squiggles in my code.
Edit:
When I add # to every server code snippet then I get squiggles, and can't compile
If I remove # from the last if statement, then I can run the app, but that piece of code is displayed back to me in the browser page.
You need to use an # here: #Html.Partial(....).
Also #foreach and #if, as Brad said.
And code lines go in brackets: #(i++)

Html.Raw() in combination with Razor?

I am trying to add a <span> to my view when my session variable is not null.
The value of Session["error"] has the right value (I checked), but there is no <span> coming in my view when it is filled while the code DOES come into the IF statement.
#if (Session["error"] != null) { Html.Raw("<span class=\"alert\"> #Session[\"error\"].ToString() <span>"); }
Plz tell me what i need to change. I am a student and new to coding.
You need to add # to write the output of the function to the response stream. In short; #Html.Raw().
However, this is not how Razor code should be used. Instead you can embed HTML directly within your if, like so:
#if (Session["error"] != null)
{
<span class="alert">#Session["error"]</span>
}

Apache2 Perl vHosts Error

I just worked through this tutorial and modified the table by adding another column. I want to check the value before adding the template script. It didn't work and the script includes the template-ssl every time. It is important that this script works with MySQL, mass vhosts is not possible.
$My::dir = #row[3];
$My::encrypted = #row[4];
if ($My::encrypted == 'ssl') {
$s->add_config(["Include /etc/apache2/sites-available/template-ssl"]);
}
else {
$s->add_config(["Include /etc/apache2/sites-available/template-def"]);
}
I think the variables doesn't work but if(#row[4] == "ssl") also fire as true every time. Even when the DataRow contains "def".
Ok, it was too simple. The error was that you compare stings with "xx" eq "yy" and numbers with 1 == 2.

Adding html in an if statement nested in a for loop

I have code like this in my razor index view:
#for (decimal hour=7m; hour<=20.5m; hour +=0.5m)
{
var item = #Model.Items.FirstOrDefault( i=> i.Hour == hour);
if (item != null)
{
<td colspan="#item.TimeBlocks">
#item.Description
</td>
hour += (item.TimeBlocks-1)* 0.5m;
}
else
{
<td>
+
</td>
}
}
For some reason, the VS2013 consistently tells me "} expected" in the Error List window. The syntax highlighter shows everything highlighted fine until the curly brace right before the else.
I have tried using #: for the html tags. It works fine in the if block, but in the else block I get the following error:
":" is not valid at the start of a code block.
I have tried wrapping the html tags in a tag as well, but that doesn't work either.
How would I do this to get it to render?
You have a mistake in your code.
The following line
var item = #Model.Items.FirstOrDefault( i=> i.Hour == hour);
Should actually read:
var item = Model.Items.FirstOrDefault( i=> i.Hour == hour);
Removing that will fix your code

When do we use "#" in Razor

I am reading a example from a book like this:
#switch ((int)ViewBag.ProductCount) {
                case 0:
                    #: Out of Stock
                    break;
                case 1:
                    <b>Low Stock (#ViewBag.ProductCount)</b>
                    break;
                default:
                    #ViewBag.ProductCount
                    break;
            }
so my question is how do we determine when and where do we need the "#" ? for example ViewBag.ProductCount does not have it but then in case 1: it does have it.
You need the # sign before the start of a code block:
#{
// code here
}
Or before a control of flow statement that appears within mHTML markup#
#if(this || that){
<h1>Hello World</h1>
}
else
{
<h1>Good bye</h1>
}
Or before a server-side statement, expression or value that needs to be rendered to the browser:
#Datetime.Now // will render the current time to the browser
#(2/2 == 1) // will render 'true' to the browser
You can read more at my site here: http://www.mikesdotnetting.com/Article/153/Inline-Razor-Syntax-Overview