在 Razor 中连接字符串 [英] Concatenating strings in Razor
本文介绍了在 Razor 中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Razor 语法中连接两个字符串?
How would I join two strings in Razor syntax?
如果我有:@Model.address
和 @Model.city
并且我希望输出是 address city
我会怎么做做?是不是像@Model.address + " " + @Model.city
这么简单?
If I had: @Model.address
and @Model.city
and I wanted the out put to be address city
what would I do? Is it as simple as doing @Model.address + " " + @Model.city
?
推荐答案
使用 Razor 的括号语法:
Use the parentesis syntax of Razor:
@(Model.address + " " + Model.city)
或
@(String.Format("{0} {1}", Model.address, Model.city))
更新:使用 C# 6,您还可以使用 $-Notation(官方内插字符串):
Update: With C# 6 you can also use the $-Notation (officially interpolated strings):
@($"{Model.address} {Model.city}")
这篇关于在 Razor 中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文