I want to update some div block next way ->
my view for update :
@Ajax.ActionLink("Show",
"About",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "updateMe"
})
<div id="updateMe"> </div>
my controller:
int count = 0;
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult About()
{
if (Request.IsAjaxRequest())
{
count++;
ViewBag.count = count;
return PartialView();
}
return RedirectToAction("Index");
}
my PartialView:
<h1>Hellow from Partial View ! @ViewBag.count </h1>
Also I have this in .config file
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
and referenced libriries:
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
Expected: first click:
Hellow from Partial View ! 1
second:
Hellow from Partial View ! 2
have: Hellow from Partial View ! 1
?
The
Controllerclass is initialised every request again and therefore the value of your fieldcountis being reinitialised every time. You would want to have this “globally”. One way is to define it as astaticfield; but be warned, this may have some unexpected behaviour (like static field in ASP.NET MVC).