C# code in razor view is badly formatted
This issue has been moved from a ticket on Developer Community.
When I have c# code in razor view like below
RazorBlock editor =
@<text>
<div class="d-flex gap-10 w-100 mh-100 h-100">
@(
Html.DevExtreme()
.DataGrid<ROS_RoomStandard>()
.SetBaseGridStandardSettigns()
.Selection(s => s.Mode(SelectionMode.Single))
.GroupPanel(g => g.Visible(false))
.Height("100%")
.Width("300px")
.ID("discount-data-grid-source")
.Columns(c => {
c.AddFor(m => m.Name);
})
)
@(
Html.DevExtreme()
.DataGrid<HOR_HotelOfferRoomDiscount>()
.SetBaseGridStandardSettigns()
.Selection(s => s.Mode(SelectionMode.Single))
.GroupPanel(g => g.Visible(false))
.Height("100%")
.Width("100%")
.ID("discount-data-grid")
.Columns(c => {
c.AddFor(c => c.RoomDiscountType).Lookup(l => l
.DataSource(roomDiscountTypes)
.ValueExpr("id")
.DisplayExpr("name")
);
c.AddFor(c => c.DaysCount);
c.AddFor(c => c.ChangePriceByPercentage).Lookup(l => {
l.DataSource(new []
{
new {
Name = Localizer.GetString(Translations.PropertyZmianaProcentowa),
Value = true
},
new {
Name = Localizer.GetString(Translations.PropertyZmianaKwotowa),
Value = false
}
});
l.DisplayExpr("Name");
l.ValueExpr("Value");
});
c.AddFor(c => c.ChangePriceValue);
})
)
</div>
</text>
And then somewhere I press ';' or I use auto format, the code looks like here
RazorBlock editor =
@<text>
<div class="d-flex gap-10 w-100 mh-100 h-100">
@(
Html.DevExtreme()
.DataGrid<ROS_RoomStandard>()
.SetBaseGridStandardSettigns()
.Selection(s => s.Mode(SelectionMode.Single))
.GroupPanel(g => g.Visible(false))
.Height("100%")
.Width("300px")
.ID("discount-data-grid-source")
.Columns(c =>
{
c.AddFor(m => m.Name);
})
)
@(
Html.DevExtreme()
.DataGrid<HOR_HotelOfferRoomDiscount>()
.SetBaseGridStandardSettigns()
.Selection(s => s.Mode(SelectionMode.Single))
.GroupPanel(g => g.Visible(false))
.Height("100%")
.Width("100%")
.ID("discount-data-grid")
.Columns(c =>
{
c.AddFor(c => c.RoomDiscountType).Lookup(l => l
.DataSource(roomDiscountTypes)
.ValueExpr("id")
.DisplayExpr("name")
);
c.AddFor(c => c.DaysCount);
c.AddFor(c => c.ChangePriceByPercentage).Lookup(l =>
{
l.DataSource(new[]
{
new {
Name = Localizer.GetString(Translations.PropertyZmianaProcentowa),
Value = true
},
new {
Name = Localizer.GetString(Translations.PropertyZmianaKwotowa),
Value = false
}
});
l.DisplayExpr("Name");
l.ValueExpr("Value");
});
c.AddFor(c => c.ChangePriceValue);
})
)
</div>
</text>
Original Comments
Feedback Bot on 29/09/2022, 00:53 PM:
(private comment, text removed)
Wenwen Fan [MSFT] on 30/09/2022, 01:07 PM:
(private comment, text removed)
Wenwen Fan [MSFT] on 30/09/2022, 01:08 PM:
(private comment, text removed)
Original Solutions
(no solutions)
This is probably the same underlying issue as https://github.com/dotnet/razor-tooling/issues/6150, as it involves lambdas, render fragments and object initializers, which are basically the worst possible things for Razor formatting, but worth logging separately because the issue is slightly different. The formatting is wrong with this, but stable, and doesn't march slowly across the screen at least.