javascriptasp.net-mvcsortingpaginationwebgrid

MVC Webgrid, javascript stops working after sort and paging


I have a problem with my webgrid, in one of my columns I have an actionlinks that has a JavaScript with a click function, everything works fine. But after I have sorted any column or paging the grid my JavaScript stops working. I have removed the ajaxupdatecontainerID property and my JavaScript works after paging but I can´t sort my columns anymore and after paging the browser scrolls to top of the page. check my code below:

<div id="grider">
    @{ var grid3 = new WebGrid(Model.webgridlastlopp.ToList(), rowsPerPage: 10, defaultSort: "Status", canPage: true, ajaxUpdateContainerId: "grider");}
    @grid3.GetHtml(tableStyle: "webgrid",
                        headerStyle: "webgrid-header",
                        footerStyle: "webgrid-footer",
                        alternatingRowStyle: "webgrid-alternating-row",
                        selectedRowStyle: "webgrid-selected-row",
                        rowStyle: "webgrid-row-style",
                        grid3.Column("Startstation", "Start station/kombiterminal"),
                                              grid3.Column("Slutstation", "Slut station/kombiterminal"),
                                              grid3.Column("Upphämtningsdatum", "Startdatum", format: @<text>@item.Upphämtningsdatum.ToString("yyyy/MM/dd")</text>),
                                              grid3.Column("Leveransdatum", "Leveransdatum", format: @<text>@item.Leveransdatum.ToString("yyyy/MM/dd")</text>),
                                              grid3.Column(format: (item) => Html.ActionLink("Visa detaljer", "OrderDetails", "Home", new { id = item.Ordernummer }, new { @class = "modal", @Style = "color:Gray;" }))
                                              ))
  </div>
  <script type="text/javascript">
        $(function () {
            $('.modal').click(function () {
                alert("hello");
        });
  </script>

Solution

  • As you page, new HTML is being loaded into the page. Because of this, you need to use the jQuery live method:

    <script type="text/javascript">
            $(function () {
                $('.modal').live('click', function () {
                    alert("hello");
            });
      </script>
    

    jQuery live