Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 连接cshtml文件中的字符串_.net_Asp.net Mvc_Asp.net Core_Razor - Fatal编程技术网

.net 连接cshtml文件中的字符串

.net 连接cshtml文件中的字符串,.net,asp.net-mvc,asp.net-core,razor,.net,Asp.net Mvc,Asp.net Core,Razor,如何在cshtml文件中连接两个c#字符串 如果我有:@Model.country和@Model.state,我希望输出是country和state,我会怎么做?有几种方法可以做到这一点 @(Model.country + " " + Model.state) 您可以使用@(Model.Country+“”+Model.State)或 使用字符串concat函数@String.concat(Model.Country,“,Model.State) 在ViewModel中

如何在cshtml文件中连接两个c#字符串


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

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

@(Model.country + " " + Model.state)
  • 您可以使用
    @(Model.Country+“”+Model.State)

  • 使用字符串concat函数
    @String.concat(Model.Country,“,Model.State)

  • 在ViewModel中添加只读属性,并使用此属性显示数据。e、 g:

      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" />
    
    <script>
            $(function () {
                var address = '@Model.Country@Model.State';
                console.log(address);
            })
        </script>
    
    @Model.Country@Model.State
    
    结果:

    如果您想在js中获得它,下面是一个演示:

        @Model.Country&nbsp;@Model.State
    <input value="@Model.Country @Model.State" />
    
    <script>
            $(function () {
                var address = '@Model.Country@Model.State';
                console.log(address);
            })
        </script>
    
    
    $(函数(){
    var address='@Model。Country@Model.State';
    控制台日志(地址);
    })
    
    结果:

    这是否回答了您的问题?您好,我的答案有用吗?详细的解释和您的答案将有助于更好地理解解决方案。请改进它。