使用Razor创建无间距的图像行

使用Razor创建无间距的图像行,razor,Razor,这似乎与类似,但我尝试了那里的建议(除了自定义帮助器),但没有任何帮助 我试图在Razor中创建一行图像,以便它们之间没有空间/间隙。我的Razor视图代码如下所示。模型是一个int string theNumber = String.Format( "{0:00000}", Model ); foreach( char theChar in theNumber.ToCharArray() ) { <img src="/images/odometer/@{@th

这似乎与类似,但我尝试了那里的建议(除了自定义帮助器),但没有任何帮助

我试图在Razor中创建一行图像,以便它们之间没有空间/间隙。我的Razor视图代码如下所示。模型是一个int

    string theNumber = String.Format( "{0:00000}", Model );

    foreach( char theChar in theNumber.ToCharArray() )
    { 
<img src="/images/odometer/@{@theChar}.gif" style="border-width: 0px;height: 20px;width: 15px;" alt="" />
    }
string theNumber=string.Format(“{0:00000}”,Model);
foreach(在number.ToCharArray()中对字符进行字符化)
{ 
}
这将生成如下所示的HTML

        <img src="/images/odometer/0.gif" style="border-width: 0px;height: 20px;width: 15px;" alt="" />
<img src="/images/odometer/0.gif" style="border-width: 0px;height: 20px;width: 15px;" alt="" />
<img src="/images/odometer/1.gif" style="border-width: 0px;height: 20px;width: 15px;" alt="" />
<img src="/images/odometer/9.gif" style="border-width: 0px;height: 20px;width: 15px;" alt="" />
<img src="/images/odometer/7.gif" style="border-width: 0px;height: 20px;width: 15px;" alt="" />

这将导致浏览器中显示以下内容

HTML源代码中的换行符导致图像之间出现间隙。我真正想要的是在一条长线上生成HTML,如下所示

<img src="images/odometer/0.gif" style="border-width:0px;height:20px;width:15px;" /><img src="images/odometer/0.gif" style="border-width:0px;height:20px;width:15px;" /><img src="images/odometer/1.gif" style="border-width:0px;height:20px;width:15px;" /><img src="images/odometer/9.gif" style="border-width:0px;height:20px;width:15px;" /><img src="images/odometer/7.gif" style="border-width:0px;height:20px;width:15px;" />

这将导致像这样的图像


我知道一个选择是不使用循环。我的数字总是五位数字,因此我不必在字符串中的每个字符上循环,我只需为每个数字写一个img标记。

如果有固定的数字并且渲染是一个问题,我要么放弃
foreach
,要么尝试在同一行上写整个语句。

我相信这是可行的:

@{
    var htmlTemplate = "<img src=\"/images/odometer/{0}.gif\" style=\"border-width: 0px;height: 20px;width: 15px;\" alt=\"\" />";
    string theNumber = String.Format("{0:00000}", DateTime.Now);
}
@foreach (char theChar in theNumber.ToCharArray())
{ 
    @Html.Raw(string.Format(htmlTemplate, theChar))
}
@{
var htmlTemplate=“”;
string theNumber=string.Format(“{0:00000}”,DateTime.Now);
}
@foreach(在number.ToCharArray()中对字符进行字符化)
{ 
@Html.Raw(string.Format(htmlTemplate,theChar))
}

HTH

这当然是一个选项,我现在已经这样做了,但我可以想象有一天它将不再是一个选项,我将被迫使用循环,因此我希望有一个更好的解决方案。