Concatenating strings in Razor

2020-02-17 08:41发布

How would I join two strings in Razor syntax?

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?

5条回答
劫难
2楼-- · 2020-02-17 08:48

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

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

查看更多
Anthone
3楼-- · 2020-02-17 08:52

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}")
查看更多
我想做一个坏孩纸
4楼-- · 2020-02-17 08:54

You can give like this....

<a href="@(IsProduction.IsProductionUrl)Index/LogOut">
查看更多
疯言疯语
5楼-- · 2020-02-17 09:04

String.Format also works in Razor:

String.Format("{0} - {1}", Model.address, Model.city)
查看更多
Juvenile、少年°
6楼-- · 2020-02-17 09:08

You can use:

@foreach (var item in Model)
{
  ...
  @Html.DisplayFor(modelItem => item.address + " " + item.city) 
  ...
查看更多
登录 后发表回答