Trying to populate table data with:
@foreach (var mc in Model.McList)
{
<tr>
<td>
@(@mc.Amount != null ? @mc.Amount @@ @mc.Price : null)
</td>
</tr>
}
but stuck in razor syntax errors:
expect :
expect )
or ambiguous invocation: void Write
Amount is int? nullable type.
@(@mc.Amount != null ? @mc.Amount : null) //works only with one property
@if (@mc.Amount != null) {@mc.Amount <text>@@</text> @mc.Price} //works
@if (@mc.Amount != null) {@mc.Amount @@ @mc.Price} //error
@if (@mc.Amount != null) {@mc.Amount @:@@ @mc.Price} //error
is it shorter way to check for null value without if?
I don't think that's possible. If you must do that in one line, the closest I get is:
@(@mc.Amount != null ? string.Format("{0} @ {1}", mc.Amount, mc.Price) : null)
I'd probably never do that myself!
I think you're trying to make something that looks like this:
5 @ 20
If so, you need to keep in mind that everything between @(
and )
is pure C# code; what you need to do is something like this:
@(mc.Amount != null ? string.Format("{0} @ {1}", mc.Amount, mc.Price) : "")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.