Reputation: 25
There is a simple solution with this issue. I have index page with partial view. partial view contains list of links to another views, "Back to List" link which gets list of links into partial view. Also index page has jQuery script with ajax which prevents page redirection and returns result into partial view. So, if I click any link first time, the result is displayed into partial view of Index page. Then I click "Back to List", and click any link second time the script is not working and page redirects. what is wrong?
Here is a code:
model "LinkList":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AjaxTest.Models
{
public partial class LinkList
{
public int id { get; set; }
public string label { get; set; }
public string method { get; set; }
public string controller { get; set; }
public string htmlLabel { get; set; }
}
}
The controller has following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AjaxTest.Models;
namespace AjaxTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult PartialView(int id)
{
ViewBag.Id = id;
return View();
}
public ActionResult PartialsList()
{
List<LinkList> list = new List<LinkList>()
{
new LinkList{id = 1, label = "First partial", method = "PartialView", htmlLabel = "first-partial"},
new LinkList{id = 2, label = "Second partial", method = "PartialView",htmlLabel = "second-partial"},
new LinkList{id = 3, label = "Third partial", method = "PartialView", htmlLabel = "third-partial"}
};
return View(list);
}
}
}
And then 3 view pages PartialView.cshtml:
This is partial view with id = (@ViewBag.Id)
PartialsList.cshtml:
@model IEnumerable<AjaxTest.Models.LinkList>
<div>
@foreach (var item in Model)
{
@Html.ActionLink(item.label, item.method, item.controller, new { id = item.id }, new { id = item.htmlLabel })
<br />
}
</div>
And index.cshtml:
@{
ViewBag.Title = "Index";
}
<script type="text/javascript">
jQuery(document).ready(function () {
$('#first-partial').click(function (event) {
event.preventDefault();
var url = $(this).attr('href');
$('#pview').load(url);
})
$('#second-partial').click(function (event) {
event.preventDefault();
var url = $(this).attr('href');
$('#pview').load(url);
})
$('#third-partial').click(function (event) {
event.preventDefault();
var url = $(this).attr('href');
$('#pview').load(url);
})
$('#backToList').click(function (event) {
event.preventDefault();
var url = $(this).attr('href');
$('#pview').load(url);
})
});
</script>
<h2>Index</h2>
<div id="index">
<fieldset>
<legend>Index</legend>
This is Index page!
</fieldset>
</div>
<div>
<fieldset>
<legend>Partials</legend>
<br />
<fieldset>
<div id="pview">
@Html.Action("PartialsList", "Home", null)
</div>
</fieldset>
<div>
@Html.ActionLink("Back to list", "PartialsList", "Home", null, new { id = "backToList" })
</div>
</fieldset>
</div>
Upvotes: 0
Views: 532
Reputation: 87203
If you want to bind events to dynamically created elements:
$(document).on('click', 'yourSelector', function () {
console.log('Event occured');
// Your event handler function call here
});
jQuery DOC: https://api.jquery.com/on/
https://stackoverflow.com/a/29728269/2025923
https://stackoverflow.com/a/29788126/2025923
Upvotes: 2