I attempting to get a numeric value to be returned from a database using linq in razor. When I attempt nothing is being returned. Here is an example of my what i did. The variable which is not printing is the "grad_year"
@{
var trust = new trusteeEntities();
var former = (from g in trust.Members
where g.status == "Former"
orderby g.lname ascending
select g);
var pos = 0;
foreach (var f in former)
{
<div class="member">
<h6>@f.fname @f.lname @if (f.grad_year != null) { @string.Format("'{0:00}", f.grad_year); } @if (f.deceased == true){@Html.Raw("**")}</h6>
</div>
}
}
The only logical conclusion is that your collection of former
is empty (Count == 0) and thus your enumeration is not executed. Determine why the collection is empty to verify.
It is probably not getting outputted because it is just executing the 'string.Format(..)' as a statement.
You have to explicity tell the Razor engine to output it.
This can be done by putting <text></text> around it, eg
@if (f.grad_year != null) { <text>@string.Format("'{0:00}", f.grad_year)</text> }
Edit:
You could also do this with the conditional operator , eg
@(f.grad_year != null ? @string.Format("'{0:00}", f.grad_year) : null)
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.