Mappan
Mappan

Reputation: 2617

Display a counter in MVC webpage

I'm trying to make a counter display in an MVC based webpage.

In the home controller I've got this simple test code:

int counter = 0;
    public ActionResult Index()
    {
        counter++;

            ViewBag.Message = counter;

        return View();
    }

Index.cshtml I have this:

<h2>@ViewBag.Message</h2>
<p>
    @Html.ActionLink("Refresh", "Index", "Home")
<br />
    @Html.ActionLink("About", "About", "Home")
</p>

The point is to display each iteration of the counter in the viewbag message. I'm pretty new to this and would love any help. I'm trying to use RedirectToAction to do this but I've got no idea how.

It just displays 1, so it does go once in the loop but then never again.

Upvotes: 1

Views: 2281

Answers (1)

IT ppl
IT ppl

Reputation: 2647

try declaring your counter variable static

for more details go through this & this

Upvotes: 6

Related Questions