简体   繁体   English

在MVC Razor中包装代码

[英]Wrap code in MVC Razor

For long lines of C# code in a ASP.NET MVC Razor view, how can you wrap the code onto the next line? 对于ASP.NET MVC Razor视图中的C#代码长行,如何将代码包装到下一行? If I add a carriage return, it renders the code as HTML. 如果添加回车,它将代码呈现为HTML。

Edit : Here's an example of the code I would like to wrap. 编辑 :这是我要包装的代码的示例。 In the last line, the added method calls start to run off the screen and I'd like to carry something like .WithAjax(true) to the next line. 在最后一行中,添加的方法调用开始在屏幕外运行,我想在下一行中携带.WithAjax(true)类的东西。

@Html.Grid(stuff).Columns(columns =>
{
    //stuff
}).Sortable().Filterable().WithMultipleFilters().WithPaging(WebSettings.DefaultPageSize)
  .WithAjax(true).Exportable(true)

You can enclose a "Razor code block" using the @{ } syntax. 您可以使用@{ }语法将“剃刀代码块”括起来。 The contents will not be rendered, but variables declared inside are accessible by the rest of the page. 内容将不会呈现,但是在内部声明的变量可由页面的其余部分访问。

@{
    string longMessage = "Lorem ipsum dolor sit amet, consectetur " +
        /* these linebreaks will not be part of the string variable */ +
        "adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.";
} 

@* Now render the string variable declared in the code block *@
<p>@longMessage</p>

See Razor syntax for ASP.NET Core . 请参阅ASP.NET Core的Razor语法

Edit 编辑

For your concrete code: 对于您的具体代码:

 @{
    // assign the IHtmlString created by the HTML Helper to a variable
    var grid = Html.Grid(stuff).Columns(columns =>
        {
          //stuff
        }).Sortable().Filterable().WithMultipleFilters()
        .WithPaging(WebSettings.DefaultPageSize)
        .WithAjax(true).Exportable(true));
 }

@* now render the variable content *@
@grid

You can use @() like this: 您可以这样使用@()

@(Html.Grid(stuff).Columns(columns =>
{
//stuff
}).Sortable().Filterable().WithMultipleFilters()
  .WithPaging(WebSettings.DefaultPageSize)
  .WithAjax(true).Exportable(true))

This is called Explicit Expression . 这称为显式表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM