How would I join two c# strings in cshtml file?
If I had: @Model.country and @Model.state and I wanted the out put to be country and state what would I do?
How would I join two c# strings in cshtml file?
If I had: @Model.country and @Model.state and I wanted the out put to be country and state what would I do?
There are a couple of ways to do that.
You can use @(Model.Country + " " + Model.State) or
with String concat function @string.Concat(Model.Country, " ", Model.State)
Add Readonly Property in ViewModel and use this property to display data. e.g:
public class IndexViewModel
{
public string Country {get;set;}
public string State {get;set;}
public string CountryWithState => string.Concat(Country," ", State);
}
Firstly,If you want to put the value to input,or only want to show them,here is a demo:
@Model.Country @Model.State
<input value="@Model.Country @Model.State" />
If you want to get it in js,here is a demo:
<script>
$(function () {
var address = '@[email protected]';
console.log(address);
})
</script>
@(Model.country + " " + Model.state)