在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,您还可以使用 $-符号(正式插入的字符串):
Update: With C# 6 you can also use the $-Notation (officially interpolated strings):
@($"{Model.address} {Model.city}")
这篇关于在Razor中串联字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文