Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Razor.NET无法识别代码脚本_Asp.net Mvc_Razor - Fatal编程技术网

Asp.net mvc Razor.NET无法识别代码脚本

Asp.net mvc Razor.NET无法识别代码脚本,asp.net-mvc,razor,Asp.net Mvc,Razor,我的视图中有以下代码。在for语句中,我想将父div中的每4项分组如果(计数器==0),我呈现父div,然后呈现四个项目,如果它是4个项目组中的最后一个,我关闭父div 但是,razor似乎希望我在第一个if中关闭父div。如果下面有几行,Razor似乎不够聪明,无法知道父结束标记在第二行中,或者可能我遗漏了什么。有什么办法解决这个问题吗 @{int counter = 0; } @for (int i = 0; i < Model.UserManagedRoles.Count; i+

我的视图中有以下代码。在
for
语句中,我想将父
div
中的每4项分组
如果(计数器==0)
,我呈现父
div
,然后呈现四个项目,如果它是4个项目组中的最后一个,我关闭父
div

但是,razor似乎希望我在第一个
if
中关闭父
div
。如果下面有几行,Razor似乎不够聪明,无法知道父结束标记在第二行中,或者可能我遗漏了什么。有什么办法解决这个问题吗

 @{int counter = 0; }
 @for (int i = 0; i < Model.UserManagedRoles.Count; i++){
      string roleName = Model.UserManagedRoles[i].Name.Replace(" ", "").Trim();
      string id = string.Format("chk{0}", roleName);

      if (counter == 0)
      {
           <div  class="rowCapture divUserRoles">
      }

      <div class="hzStackSmall">
      <input type="checkbox" id="@id" name="@id" value="@roleName" />@Model.UserManagedRoles[i].Name</div>

      if(counter == 3 || i == Model.UserManagedRoles.Count) {
           </div>
      }

      counter++;
      if (counter == 4)
      {
           counter = 0;
      }
 }
@{int counter=0;}
@对于(int i=0;i
您必须使用razor引擎使用
@:
打印HTML代码。这有用吗

我强烈推荐ScottGu博客中关于razor引擎的教程

 @{int counter = 0; }

 @for (int i = 0; i < Model.UserManagedRoles.Count; i++)
 {
    string roleName = Model.UserManagedRoles[i].Name.Replace(" ", "").Trim();
    string id = string.Format("chk{0}", roleName);

    if (counter == 0)
    {
       @:<div  class="rowCapture divUserRoles">
    }

    @:<div class="hzStackSmall">
    @:<input type="checkbox" id="@id" name="@id" value="@roleName" />@Model.UserManagedRoles[i].Name</div>

    if(counter == 3 || i == Model.UserManagedRoles.Count) {
       @:</div>
    }

    counter++;
    if (counter == 4)
    {
       counter = 0;
    }
 }
@{int counter=0;}
@对于(int i=0;i
@tarkanboy,很乐意帮忙。你能把我的回答(或任何其他回答)作为一个答案吗?@cacho-这种情况太频繁了。