Razor - Mutate布尔变量

时间:2018-06-11 03:05:18

标签: asp.net-mvc razor

我在cshtml文件中声明了一个布尔变量。

@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>

然而,Visual Studio给了我一些错误:

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

好像我不能直接分配&#39; true&#39;当试图改变它。如果是这样的话,我应该怎么做?

0 个答案:

没有答案