Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VisualStudio auto-format doesn't format my razor code correctly

I'm using @:</div> to display some bootstrap columns correctly. This is the my code where i'm using it:

var i = 0;

<div class="container-fluid">
    <div class="row show-grid">
        @foreach (var one in Model)
        {

            if (i % 3 == 0)
            {
                @:<div class="row show-grid">
            }

            <div class="one-element col-md-4">
                @one.Title
            </div>

            if ((i + 1) % 3 == 0)
            {
                @:</div>
            }

            i++;
        }
    </div>
</div>

It formats this (which works fine as long as i don't use the VisualStudio auto-format feature):

@:</div>

to this:

@:
</div>

And then the application doesn't work anymore.

How can this be fixed?

like image 937
Jo Smo Avatar asked Aug 20 '15 03:08

Jo Smo


People also ask

How do I fix Visual Studio formatting code?

On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.

How do I Auto beautify code in Visual Studio?

Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.

How do I automatically format code in Visual Studio Code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How do I change the format of a file in Visual Studio Code?

VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.


1 Answers

I fixed it by using @Html.Raw() like this:

var i = 0;

<div class="container-fluid">
    <div class="row show-grid">
        @foreach (var one in Model)
        {

            if (i % 3 == 0)
            {
                @Html.Raw("<div class=\"row show-grid\">")
            }

            <div class="one-element col-md-4">
                @one.Title
            </div>

            if ((i + 1) % 3 == 0)
            {
                @Html.Raw("</div>")
            }

            i++;
        }
    </div>
</div>

I guess that this is as good as it gets.

But if anyone knows of a more elegant way to do it, please let me know.

like image 78
Jo Smo Avatar answered Dec 11 '22 07:12

Jo Smo