.net 连接cshtml文件中的字符串
如何在cshtml文件中连接两个c#字符串.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中
如果我有:@Model.country和@Model.state,我希望输出是country和state,我会怎么做?有几种方法可以做到这一点
@(Model.country + " " + Model.state)
@(Model.Country+“”+Model.State)
或
@String.concat(Model.Country,“,Model.State)
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 @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';
控制台日志(地址);
})
结果:
这是否回答了您的问题?您好,我的答案有用吗?详细的解释和您的答案将有助于更好地理解解决方案。请改进它。