How can I use ViewBag
as counter?
WHAT DO I NEED?
I'm making phone numbers. When I add a new phone, I send my View
to my Controller
and it returns me to PartialView
of my Inputs
.
I needed a counter, how many times I added the phone, to reference the Input
, for example, I added a phone:
<div class="telefone1"></div>
I added another
<div class="telefone2"></div>
Here is where my difficulty comes in, a counter in MVC
AJAX CALL
$.ajax({
url: '/Controller/AddTelefone',
success: function (partialView) {
$('#div').append(partialView);
}
});
MY CONTROLLER
//variavel global
public int count = 0;
public ActionResult AddTelefone()
{
ViewBag.count = count + 1;
return PartialView("partialview");
}
MY VIEW
@{
var thing = ViewBag.count;
}
@Html.Raw(thing)
ERROR!
My ViewBag.count
is always with the value: 1
.