2
Is there any way I can use the ViewBag
as an accountant?
WHAT I NEED?
I’m making phone registrations. When I add a new phone, I send my View
for my Controller
and he returns me to PartialView
of my Inputs
.
Needed an accountant, 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>
where my difficulty comes in, an accountant in MVC
CALLED AJAX
$.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
.
Have you tried to create a variable within cshtml to receive Viewbag and counter that variable?
– Danilo Pádua
Post as you are calling the partialView with the
inputs
. I think you must be using ajax.– Randrade
Yes @Randrade on my call
Ajax
I send him toController
and he returns toPartialView
– Furlan
Add your ajax code too
– Randrade
ready @Randrade changed the question with the code
Ajax
– Furlan
That counter you need for what? Will send it to some
controller
in the end?– Randrade
not @Randrade only integration with
jQuery
, for example, exclude such an element from adiv
, severalid
– Furlan
Let’s go continue this discussion in chat.
– Furlan