我在_Layout.cshtml页面中有if else循环但是,当我尝试运行它时,它给出了RenderBody错误说,Renderbody已被调用

时间:2017-04-24 12:54:37

标签: c# asp.net-mvc razor

我已经附上了下面的代码,请看,我是一个新手,如果你在代码中发帖,那将是非常好的...

 <!DOCTYPE html>
 <html>
 <head>
 @model Forecaster.Models.ForecastModel
 <meta charset="utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>@ViewBag.Title - Forecaster</title>
 @Styles.Render("~/Content/css")
 @Scripts.Render("~/bundles/modernizr")
 @Scripts.Render("~/bundles/jquery")
 </head>
 <body>
  @for (int i = 0; i < Model.UsersInfoList.Count(); i++)
  {
    <div class="navbar" role="main" style="margin-bottom:-50px;">
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><img src="~/Images/logo.jpg" alt="Forecaster" style="padding-left:inherit;"></a>
        </div>
        @if ((Model.UsersInfoList[i].RoleId == 1) && (Model.UsersInfoList[i].UserName == Session["UserName"].ToString()))
        {
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("HOME", "Dashboard", "Dashboard")</li>
                <li>@Html.ActionLink("Employee", "Index", "EmployeeMasters")</li>
                <li>@Html.ActionLink("Project", "Index", "ProjectMasters")</li>
                <li>@Html.ActionLink("Export", "ExportToExcel", "Dashboard")</li>
            </ul>
            <ul class="nav navbar-nav" style="float:right; padding-right:59px;">
                <li>
                    @Html.ActionLink("Logout", "UserDashboard", "Login")
                </li>
            </ul>
            <p class="navbar-text navbar-right">
                <a href="#">
                    <span class="glyphicon glyphicon-user"></span> @if (Session["UserName"] != null)
                    {<text>@Session["UserName"].ToString() </text> }
                </a>
            </p>
        }
        else
        {
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("HOME", "Dashboard", "Dashboard")</li>
                <li>@Html.ActionLink("Project", "Index", "ProjectMasters")</li>
            </ul>
            <ul class="nav navbar-nav" style="float:right; padding-right:59px;">
                <li>
                    @Html.ActionLink("Logout", "UserDashboard", "Login")
                </li>
            </ul>
            <p class="navbar-text navbar-right">
                <a href="#">
                    <span class="glyphicon glyphicon-user"></span> @if (Session["UserName"] != null)
                    {<text>@Session["UserName"].ToString() </text> }
                </a>
            </p>
        }
    </div>
    <div class="container" style="margin-top:45px;">
        @RenderBody()
        <hr />
        <div class="col-lg-pull-10">
            <footer></footer>
        </div>
    </div>
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
}

请有人告诉我,我做错了什么?我需要在两种情况下都授权用户和renderbody! 三江源!

3 个答案:

答案 0 :(得分:1)

你在FOR循环中有RenderBody ......

@for (int i = 0; i < Model.UsersInfoList.Count(); i++)

你确定Model.UsersInfoList中只有一条记录吗?

答案 1 :(得分:0)

你的循环一直到最后,所以@RenderBody不止一次出现。

答案 2 :(得分:-2)

你的下一部分应该从foreach循环出来。

Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities()