I have a problem with jQuery Datatables plugin in mvc.
I have a view with ajax-form whitch load a partial view with table on sumbit.
View
...
@using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions { UpdateTargetId = "myList" }, new { @id = "searchForm", @class = "form-horizontal" }))
{
// some html
}
<div id="myList">
</div>
Partial View with table
@model IEnumerable<Item>
<table id="productsTable">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(x => item.Name)</td>
<td>@Html.DisplayFor(x => item.Description)</td>
<td>
<div>
<a href="@Url.Action("Edit", "MyController", new {id = item.Id})">
</a>
<a href="@Url.Action("Delete", "MyController", new {id = item.Id})" >
</a>
</div>
</td>
</tr>
}
</tbody>
</table>
The problem is that if I put script
$(document).ready(function() {
$('#productsTable').DataTable();
});
in main View it will be not work because partial view load by ajax. And if I put this script in partial view it will be work with delay: at first user will see simple html table and than it will be convert to datatable.
Is there any way to correct load partial view with datatable? Thanks for advices.
$('#productsTable').DataTable();outside of$(document).ready(function(){}?