asp.net-mvc 在 MVC Razor 视图页面中使用 string.Format
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15441584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using string.Format in MVC Razor view page
提问by panjo
I want to render images in a Razor view using string.Formatlike this ...
我想像这样在 Razor 视图中渲染图像string.Format......
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.
这里显然有问题,因为在呈现此页面时,我在此部分中没有任何内容。
回答by SLaks
string.Format()returns a string, which you're discarding.
string.Format()返回一个字符串,您正在丢弃该字符串。
You need to print that string to the page:
您需要将该字符串打印到页面:
@string.Format(...)
Note that since this isn't a statement, there shouldn't be a ;.
请注意,由于这不是语句,因此不应该有;.
Also note that it would be better to use Razor itself:
另请注意,最好使用 Razor 本身:
<img src="@p.Path" width="100" alt="@p.AlternateText" />
回答by Callum Linington
I just simply do this to get around that problem:
我只是简单地这样做来解决这个问题:
@model UXLab.Areas.SectionArea.ViewModels.SectionViewModel
<section>
<header>@Model.Title</header>
<p>
@{var contentBuilder = new System.Text.StringBuilder(); }
@foreach (var link in Model.Links)
{
contentBuilder.append(Html.ActionLink(link.LinkText, link.Action,
link.Controller));
}
@Html.Raw(contentBuilder.ToString())
</p>
</section>
In this example, I loop through some links I want to display to the page which are stored in a ViewModel.
在这个例子中,我循环浏览一些我想显示到页面的链接,这些链接存储在ViewModel.
In order to display the Links on the page, I loop through them all appending them to a StringBuilder, then use Html.Rawto display the raw Html, if you don't use Rawthen you'll not get quotes and things through to the page example:
为了在页面上显示链接,我循环遍历它们,将它们全部附加到 a StringBuilder,然后用于Html.Raw显示原始 Html,如果您不使用,Raw那么您将无法通过页面示例获得引号和内容:
1: @String.Format("\"Hello {0}\"", Model.Name)
2: @Html.Raw(String.Format("\"Hello {0}\"", Model.Name))
Line 1 will display " Hello " Melman
Line 2 will display "Hello Melman"
第 1 行将显示" Hello " Melman
第 2 行将显示"Hello Melman"
Just some stuff I have found out when playing with outputting to the page. The basic idea is that, you build the page html up, then display it. So a store as you go method, once you finished manipulating the html output, you then display it using @outsite of any {}
只是我在玩输出到页面时发现的一些东西。基本思想是,您将页面 html 构建起来,然后显示它。因此,一种随用随存储方法,一旦您完成对 html 输出的操作,您就可以使用@任何外部站点显示它{}
回答by Esteban
It's true using string.Formatis painful in Razor, but you dohave an Html.FormatValue, here is how to use it:
string.Format在 Razor 中使用确实很痛苦,但是您确实有一个Html.FormatValue,这里是如何使用它:
@Html.FormatValue("value", "format")
The method signature is:
方法签名是:
FormatValue(object value, string format)
回答by juFo
I had the same problem and I've done it like this using Url.Content and string.Format:
我遇到了同样的问题,我已经使用 Url.Content 和 string.Format 这样做了:
<img src="@Url.Content(string.Format("~/Content/img/{0}", Resources.MyImage))" />

