微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 cshtml 文件中连接字符串

如何解决在 cshtml 文件中连接字符串

如何在 cshtml 文件中加入两个 c# 字符串?

如果我有:@Model.country 和 @Model.state 并且我希望输出是国家和州,我会怎么做?

解决方法

有几种方法可以做到这一点。

  1. 您可以使用 @(Model.Country + " " + Model.State)

  2. 用字符串连接函数@string.Concat(Model.Country," ",Model.State)

  3. 在 ViewModel 中添加 Readonly 属性并使用该属性来显示数据。例如:

      public class IndexViewModel
      {
          public string Country {get;set;}
          public string State {get;set;}
          public string CountryWithState => string.Concat(Country,State);
      }
    
,

首先,如果你想输入值,或者只想显示它们,这里有一个演示:

    @Model.Country @Model.State
<input value="@Model.Country @Model.State" />

结果: enter image description here

如果你想在js中得到它,这里有一个演示:

<script>
        $(function () {
            var address = '@Model.Country@Model.State';
            console.log(address);
        })
    </script>

结果: enter image description here enter image description here

,
@(Model.country + " " + Model.state)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。