ASP.NET MVC剃刀多余的空格呈现 [英] ASP.NET MVC Razor extra whitespace rendered

查看:117
本文介绍了ASP.NET MVC剃刀多余的空格呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Asp.net MVC,剃刀插入文本块之间的额外空间。我想呈现一个列表是这样的:1,2,3,但得到1,2,3

  @for(INT I = 1;我3;;我++)
{
  <文字和GT; @ I< /文字和GT;
  如果(ⅰ!= 2)
  {
    <文本&GT中,< /文字和GT;
  }
}

有去除多余的空格什么办法?


解决方案

  

我要呈现的列表是这样的:1,2,3


快速和肮脏的:

  @的string.join(,,Enumerable.Range(1,3))

显然,一个自定义的助手似乎更适合在格式化视图东西的工作:

 公共静态类HtmlExtensions
{
    公共静态IHtmlString FormatList(这个HTML的HtmlHelper,IEnumerable的< INT>清单)
    {
        返回MvcHtmlString.Create(的string.join(,,列表));
    }
}

,然后简单:

  @ Html.FormatList(Model.MyList)

In Asp.net MVC, 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>
  }
}

Is there any ways to remove extra whitespace ?

解决方案

I want to render a list this way: "1, 2, 3"

Quick and dirty:

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

Obviously a custom helper seems more appropriate to the job of formatting 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 simply:

@Html.FormatList(Model.MyList)

这篇关于ASP.NET MVC剃刀多余的空格呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆