在 Razor 中连接字符串

如何在 Razor 语法中连接两个字符串?

如果我有: @Model.address@Model.city,我想输出为 address city,我会怎么做?是不是和做 @Model.address + " " + @Model.city一样简单?

133697 次浏览

Use the parentesis syntax of 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):

@($"{Model.address} {Model.city}")

String.Format also works in Razor:

String.Format("{0} - {1}", Model.address, Model.city)

the plus works just fine, i personally prefer using the concat function.

var s = string.Concat(string 1, string 2, string, 3, etc)

You can use:

@foreach (var item in Model)
{
...
@Html.DisplayFor(modelItem => item.address + " " + item.city)
...

You can give like this....

<a href="@(IsProduction.IsProductionUrl)Index/LogOut">