Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3 Razor: Is it possible to have C# code blocks without @if or @foreach?

I havent really found a solution seaching through SO.

...and suspect I should really do this in the Model...

but is it possible to have C# code blocks where adhoc code can be added eg:

@int daysLeft = CurrentTenant.TrialExpiryDate.Subtract(DateTimeOffset.Now).Days
@if (daysLeft <= 0) {
{
   <text>
   Trial period completed  
   </text>
}
else
{
   <text>
   You have @daysLeft days left of you trial
   </text>
}
like image 304
Mark Redman Avatar asked Jun 15 '11 16:06

Mark Redman


2 Answers

Sure it is:

@{
    var one = 1;
    var two = one + one;
}

Phil Haack has a pretty popular blog post summing up Razor syntax.

like image 175
Jon Avatar answered Sep 28 '22 04:09

Jon


You can create functions in razor which is what I believe you are looking for.

Another explanation.

like image 27
Kevin LaBranche Avatar answered Sep 28 '22 03:09

Kevin LaBranche