Why am I getting no output from this code if bellow it I add @{ Session.Remove("errors"); }

            @if (Session["errors"] != null)
            {

                <div class="alert alert-danger">
                    <ul>
                        @{
                            String[] errors = (String[])Session["errors"];
                        }
                        @foreach (String error in errors)
                        {
                            <li>@error</li>
                        }
                    </ul>
                </div>
            }

            @if (Session["success"] != null)
            {
                <div class="alert alert-success">
                    @Session["success"]
                </div>
            }

Does code first get evaluated and then outputted but even then it makes no sense. Without @{ Session.Remove("errors"); } I get the output with it nothing it's very annoying.

Trying to do FLASH messages that persist for only the current request.

Updated:

        TempData["errors"] = new String[] { "You need to be logged in to access this page." };

View:

        @if (TempData.ContainsKey("errors"))
        {

            <div class="alert alert-danger">
                <ul>
                    @{
                        String[] errors = (String[])TempData["errors"];
                    }
                    @foreach (String error in errors)
                    {
                        <li>@error</li>
                    }
                </ul>
            </div>
        }

        @if (TempData.ContainsKey("success"))
        {
            <div class="alert alert-success">
                @TempData["success"]
            </div>
        }

Related posts

Recent Viewed