I've been struggling with this for a while and i do not know how to solve it.
I decided to use a DATATABLE but i could not make it work on my previous model, everything works just fine but the EDIT button is not even working. Dont want to use AJAX or something like that, just the information retrieved in my FOREACH
This is my code (do NOTE that i used a foreach):
@model IEnumerable<WebApplication.Models.Approval>
@{
ViewData["Title"] = "Approvals";
}
<h1>Approvals</h1>
<table class="table" id="Exampledatatable">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.EMAIL)
</th>
<th>
@Html.DisplayNameFor(model => model.FIRSTNAME)
</th>
<th>
@Html.DisplayNameFor(model => model.LASTNAME)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.EMAIL)
</td>
<td>
@Html.DisplayFor(modelItem => item.FIRSTNAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.LASTNAME)
</td>
<td>
@Html.ActionLink("EDIT", "Edit", new { id = item.code })
</td>
</tr>
}
</tbody>
</table>
The problem is in my script
I tried to use something like this:
<script type="text/javascript">
$(document).ready(function () {
$('#myDataTable').dataTable({
aoColumns: [
null, // first column (CODE)
null, // second column (EMIAL)
null, // third (FIRSTNAME)
null, // fourth (LASTNAME)
{ // fifth column (Edit link)
"sName": "CODE",
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj)
{
// oObj.aData[0] returns the CODE
return "<a href='/Edit?id="
+ oObj.aData[0] + "'>Edit</a>";
}
}
]
});
});
</script>
How can i make it work? Can help me?
EDIT:
@model IEnumerable<WebApplication.Models.Approval>
@{
ViewData["Title"] = "Approvals";
}
<h1>Approvals</h1>
<table class="table" id="Exampledatatable">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.EMAIL)
</th>
<th>
@Html.DisplayNameFor(model => model.FIRSTNAME)
</th>
<th>
@Html.DisplayNameFor(model => model.LASTNAME)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.EMAIL)
</td>
<td>
@Html.DisplayFor(modelItem => item.FIRSTNAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.LASTNAME)
</td>
<td>
@Html.ActionLink("EDIT", "Edit", new { id = item.code })
</td>
</tr>
}
</tbody>
</table>
I am using this but it does not work on Jquery datatables:
<td>
@Html.ActionLink("EDIT", "Edit", new { id = item.code })
</td>
If i run this code, my application works just fine:
@model IEnumerable<WebApplication.Models.Approval>
@{
ViewData["Title"] = "Approvals";
}
<h1>Approvals</h1>
<table class="table" id="Exampledatatable">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.EMAIL)
</th>
<th>
@Html.DisplayNameFor(model => model.FIRSTNAME)
</th>
<th>
@Html.DisplayNameFor(model => model.LASTNAME)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.EMAIL)
</td>
<td>
@Html.DisplayFor(modelItem => item.FIRSTNAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.LASTNAME)
</td>
<td>
@Html.ActionLink("EDIT", "Edit", new { id = item.code })
</td>
</tr>
}
</tbody>
</table>
The problem comes when i try to implement the DATATABLE using JS and the HTML ACTION LINK.
@section scripts{
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.23/af-2.3.5/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.23/af-2.3.5/datatables.min.js"></script>
<script>
$(document).ready(function () {
$('#Exampledatatable').DataTable({
})
});
</script>
Not matter what i try cannot make it work