在MVC Razor视图页面中使用string.Format [英] using string.Format in MVC Razor view page

查看:128
本文介绍了在MVC Razor视图页面中使用string.Format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样使用string.Format在Razor视图中渲染图像...

I want to render images in a Razor view using string.Format like this ...

foreach (var p in @Model.Photos)
{                        
    string.Format("<img src='{0}' width='100' alt='{1}' />", p.Path, 
                                 p.AlternateText);                    
}

这显然是错误的,因为在呈现此页面时,本节中没有任何内容.

Something is clearly wrong here, because on rendering this page, I've got nothing inside this section.

推荐答案

string.Format()返回要丢弃的字符串.

string.Format() returns a string, which you're discarding.

您需要将该字符串打印到页面上:

You need to print that string to the page:

@string.Format(...)

请注意,由于这不是语句,因此不应该包含;.

Note that since this isn't a statement, there shouldn't be a ;.

还请注意,使用Razor本身会更好:

Also note that it would be better to use Razor itself:

<img src="@p.Path" width="100" alt="@p.AlternateText" />  

这篇关于在MVC Razor视图页面中使用string.Format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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