Razor - Mutate boolean variable

0

I have declared a boolean variable in my cshtml file.

@model My.Project.Web.Models.EmailViewModel

@{bool hasRec = false;}
@foreach (var item in @Model.ContentViewModel) 
{
    if (@item.Rec != null && @item.Rec != "") {
        @hasRec = true;
    }
}
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<body>
//tags are populated based on @hasRec variable.
</body>
</html>

However, Visual Studio gave me some error:

RazorEngine.Templating.TemplateCompilationException
  HResult=0x80131500
  Message=Errors while compiling a Template.
Please try the following to solve the situation:
  * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a definition for 'member': 
        try another modelType (for example 'null' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
 - error: (7, 20) Invalid expression term '='
     - error: (7, 22) ; expected

Seems like I cannot directly assign 'true' when trying to mutate it. If that's the case, how should I do it?

asp.net-mvc
razor
asked on Stack Overflow Jun 11, 2018 by xzk

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0