Visual Studio formatting doesn't work with Control + K+D and multiline
Asked Answered
I

3

10

I am using Telerik's UI for ASP.NET widgets. Most of these widgets have multiple configuration options. In .cshtml file i configure these widgets on multiple lines for readability.
For example below is the configuration for Grid widget.

<div class="row">
    <div class="col-md-12">
        @(Html.Kendo().Grid<ResultModel>()
            .Name("SearchGrid")            
            .Columns(col =>
            {
                col.Bound(p => p.DocumentID);
                col.Bound(p => p.UploadDate);
                col.Bound(p => p.DocumentType);
                col.Bound(p => p.ProcessStatus);                
                col.Bound(p => p.StateProvince);
                col.Bound(p => p.Error);                
                col.Bound(p => p.Notes);
            })
            .AutoBind(false)
            .Pageable()
            .Sortable()
            .Resizable(resize => resize.Columns(true))
            .Scrollable()
            .Sortable(sortable => sortable
                .AllowUnsort(true)
                .SortMode(GridSortMode.MultipleColumn))
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(50)
                .ServerOperation(true)
                .Read(read => read.Action("Search", "Search"))
       ).Deferred())
    </div>
</div>

After editing cshtml file i press Control + K + D to auto format. Visual studio properly formats the html and anything configured in one line. But any widget that is configured on multiple lines gets indented by one tab. So in above case everything from .Name("SearchGrid") to ).Deferred())) gets indented by one tab.

The problem is every time i edit cshtml i press Control + K + D to format edited cshtml, but that causes all other widgets to indent by one tab. and eventual all these widgets move to extreme right of the page

Invasion answered 31/1, 2018 at 21:16 Comment(6)
Yeah, this is very annoying. I wish I could somehow put an indicator or something not to reformat certain chunks of code.Bijection
@SteveGreene I haven't received any reply from anyone so i thought i am the only one who is having this annoying issue :)Invasion
I have found that if I add something to a kendo widget, it autoformats so I have gotten into the habit of doing an immediate CTRL-Z which undoes the formatting, but leaves my change. Checking if resharper might help.Bijection
Anyone found a solution? Its pretty damn annoying.Hellenize
Yeah, this has been an issue since I last used telerik controls about 4 years ago. Seems like it's still going on in 2018 :-/Velvety
@Hellenize Solution coming in VS 2019: developercommunity.visualstudio.com/content/problem/323902/…Bijection
C
0

I found a solution. Use { } after the @ for multiline Kendo Razor and it's unaffected by multiple Control + k + d.

@{
(Html.Kendo().DropDownList()
.Name("animatorEntityDropDownList")
.DataTextField("EntityName")
.DataValueField("EntityID")
.OptionLabel("- Select -")
.Events(e =>
{
e.Select("onSelectAnimatorEntityDropDownList");
})
.HtmlAttributes(new { style = "width:300px" }))
}
Cracknel answered 17/6, 2018 at 22:29 Comment(2)
Me neither, the code doesn't show any errors but when I load the page the grids aren't showing.Velvety
@HunterNelson Solution coming in VS 2019 developercommunity.visualstudio.com/content/problem/323902/…Bijection
S
0

If you use non-rendering code block, you should call Render method.

@{
    Html.Kendo().NumericTextBox()
        .Name("myTextbox").Format("#")
        .Render();
}

VS 2017 won't indent the lines in the non-rendering code block when you auto format the document. (curly brackets)

@(
    Html.Kendo().NumericTextBox()
        .Name("myTextbox").Format("#")
)

VS 2017 will indent the lines in a regular block when you auto format the document. (round brackets)

Sleepyhead answered 21/9, 2019 at 15:23 Comment(0)
P
0

I work with VS2017 (asp.net core 2.2), use Devextreme controls and have exactly the same problems.
The only (very ugly and bad) "workaround" I found is, to write the whole code on ONE line. Details see link below: Link to SO posting

Prohibition answered 30/1, 2020 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.