ASP.NET MVC Razor Adds Extra Spaces

In Asp.net, MVC Razor adds extra space between text blocks. I want to make the list like this: "1, 2, 3", but get "1, 2, 3".

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

Is there a way to remove extra spaces?

+5
source share
5 answers

I want to make the list like this: "1, 2, 3"

Quick and dirty:

@string.Join(", ", Enumerable.Range(1, 3))

Obviously, the user assistant is more suitable for setting the formatting of something in the view:

public static class HtmlExtensions
{
    public static IHtmlString FormatList(this HtmlHelper html, IEnumerable<int> list)
    {
        return MvcHtmlString.Create(string.Join(", ", list));
    }
}

and then just:

@Html.FormatList(Model.MyList)
+6
source

, ( ) :

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

Darin answer, , , :

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

, , StringBuilder, @stringBuilderObject.ToString().

+1

. , :

  1
    , 
  2
    , 
  3

, , ( ).

StringBuilder string.Join - , , , . - , , . , ol/ul li s.

0

, Razor, .

  • FireBug ( Chrome - ) , .
  • In css file try adding

text {margin: 0}

-3
source

All Articles