asp.net-mvc 在 Razor 中连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16106196/
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
Concatenating strings in Razor
提问by TheWebs
How would I join two strings in Razor syntax?
如何在 Razor 语法中连接两个字符串?
If I had: @Model.addressand @Model.cityand I wanted the out put to be address citywhat would I do? Is it as simple as doing @Model.address + " " + @Model.city?
如果我有:@Model.address并且@Model.city我希望输出是address city我会怎么做?做起来这么简单@Model.address + " " + @Model.city吗?
回答by Stephen Reindl
Use the parentesis syntax of Razor:
使用 Razor 的括号语法:
@(Model.address + " " + Model.city)
or
或者
@(String.Format("{0} {1}", Model.address, Model.city))
Update: With C# 6 you can also use the $-Notation(officially interpolated strings):
更新:使用 C# 6,您还可以使用$-Notation(官方内插字符串):
@($"{Model.address} {Model.city}")
回答by Simon
String.Format also works in Razor:
String.Format 也适用于 Razor:
String.Format("{0} - {1}", Model.address, Model.city)
回答by Sheriff
You can give like this....
你可以这样给....
<a href="@(IsProduction.IsProductionUrl)Index/LogOut">
回答by Pajoc
You can use:
您可以使用:
@foreach (var item in Model)
{
...
@Html.DisplayFor(modelItem => item.address + " " + item.city)
...
回答by d384
the plus works just fine, i personally prefer using the concat function.
加号效果很好,我个人更喜欢使用 concat 函数。
var s = string.Concat(string 1, string 2, string, 3, etc)
var s = string.Concat(string 1, string 2, string, 3, etc)

