asp.net mvc razor extra space

2020-02-26 03:09发布

Razor inserts extra space between text blocks. I want to render a list this way: "1, 2, 3" but get "1 , 2 , 3".

@for (int i = 1; i < 3; i++)
{
  <text>@i</text>
  if (i != 2)
  {
    <text>, </text>
  }
}

Are there any ways to remove this extra space?

5条回答
Explosion°爆炸
2楼-- · 2020-02-26 03:43

Any of the following should work, depending on where your values actually come from:

@string.Join(", ", myList)
@string.Join(", ", 1, 2, 3)
@string.Join(", ", Enumerable.Range(1, 3))
查看更多
叼着烟拽天下
3楼-- · 2020-02-26 03:45

You could use @Html.Raw. The code is more readable and the output doesn't have extra whitespace

@for (int i = 1; i < 3; i++)
{
  @Html.Raw(i)
  if (i != 2)
  {
    @Html.Raw(", ")
  }
}
查看更多
贼婆χ
4楼-- · 2020-02-26 03:47

Since this still a problem with the <text> tag in MVC 3 RTM + Tools Update and it can be a real headache to deal with, an alternative to eddiegroves' approach of removing whitespace from the code formatting is to avoid the use of the <text> tag altogether.

First, here is a rewrite of the original code that reproduces the problem and actually prints "1 , 2 , 3":

    @for (int i = 1; i <= 3; i++) {
      @i
      if (i < 3) {
        <text>, </text>
      }
    }

Here are four alternatives that print "1, 2, 3" instead of "1 , 2 , 3", but preserve code formatting by using @something instead of <text>.

Solution #1: Using @("")

@for (int i = 1; i <= 3; i++) {
    @i
    if (i < 3) {
        @(", ")
    }
}

Solution #2: Using @var

@for (int i = 1; i <= 3; i++) {
    var s = i < 3 ? ", " : null;
    @i @s
}

Solution #3: Using @(expression)

@for (int i = 1; i <= 3; i++) {
    @i @(i < 3 ? ", " : null)
}

Solution #4: Using @helpers

@helper Item(int index) {
    @index
}

@helper Separator(int index, int count) {
    if (index < count) {
        @(", ")
    }
}

@for (int i = 1; i <= 3; i++) {
    @Item(i) @Separator(i, 3)
}

That last one is obviously overkill for the example, but might be a useful pattern for more complicated items and separators.

查看更多
手持菜刀,她持情操
5楼-- · 2020-02-26 03:51

I would probably write a custom helper for this:

public static MvcHtmlString RenderNumbers(this HtmlHelper htmlHelper, int count)
{
    var text = string.Join(", ", Enumerable.Range(1, count).ToArray());
    return MvcHtmlString.Create(text);
}

and then use it in my view:

@Html.RenderNumbers(3);
查看更多
家丑人穷心不美
6楼-- · 2020-02-26 03:56

I believe that there is an issue in the ASP.NET Razor RC that unfortunately will treat whitespace inside the "code context" as literal white space to write to the response.

The above example is "fixed" by removing the whitespace inside the code blocks:

@for (int i = 1; i < 3; i++)
{
  <text>@i</text>if (i != 2)
{
<text>, </text>
}
}

Or more tidy:

@for (int i = 1; i < 3; i++)
{
  <text>@i</text>if(i != 2){<text>, </text>}
}

By following this thread on the asp.net site there is a discussion which has a similar issue and Andrew Nurse responds

This bug has been logged and will be considered for RTM.

So if this is the same issue, hopefully it made the list to be fixed.

This bug did not make the RTM

查看更多
登录 后发表回答