I was using MVC table to initially display the data to the user but because the data was quite large and started throwing storage out of memory
exception, i decided to switch to DataTables
. The data is displayed fine but on the postback, no data is coming over to the HTTPPost action method. I understand that MVC View had hidden views to store the values and during the postback they bind the model but how will we replicate this behaviour with a datatable?
thank you
@model IList<ViewModels.ReferralViewModel>@model IList<ViewModels.ReferralViewModel>
@{
Layout = "";
}
@if (Model != null)
{
<script src="~/Scripts/DataTables/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="~/Scripts/DataTables/dataTables.bootstrap.js" type="text/javascript"></script>
<link href="~/Content/DataTables/css/dataTables.bootstrap.css" />
<script type="text/javascript">
var editor;
$('#example').DataTable({
"serverSide": true,
"processing": true,
"order": [[1, 'asc']],
"ajax": {
"url": "/SMS/ReferralDetailsTest",
"type": "POST",
"dataType": "json"
},
'columnDefs': [{
'targets': 0,
'searchable': false,
'orderable': false,
'className': 'dt-body-center',
'render': function (data, type, full, meta) {
return '<input type="checkbox" name="IsSelected[]">';
}
},
{
'targets': 17,
'data': null,
'defaultContent': "<input type=\"text\" size=\"100\" style = \"width: 95px; padding: 6px 2px 6px 2px\" >"
},
{
'targets': 19,
'data': null,
'defaultContent': "<input type=\"button\" value=\"SMS\" class=\"btn btn - primary\" onclick=\"return $('#sendSMS').click();\" />"
}
],
"columns": [
{
"data": null,
"defaultContent": '',
"className": 'select-checkbox',
"orderable": false,
"title": "Select All"
},
{ "title": "Referral Id", "data": "ReferralId", "autoWidth": true },
{ "title": "First Name", "data": "patient.PatientFirstName", "autoWidth": true },
{ "title": "Last Name", "data": "patient.PatientLastName", "autoWidth": true },
{ "title": "Referral Date", "data": "ReferralDate", "autoWidth": true },
{ "title": "Referral Priority", "data": "ReferralPriority", "autoWidth": true },
{ "title": "Referral HospCode", "data": "ReferralHospCode", "autoWidth": true },
{ "title": "Referral Specialty Code", "data": "ReferralSpecialtyCode", "autoWidth": true },
{ "title": "Referral Specialty Name", "data": "ReferralSpecialtyName", "autoWidth": true },
{ "title": "Referral Clinic Code", "data": "ReferralClinicCode", "autoWidth": true },
{ "title": "Referral Clinic Description", "data": "ReferralClinicDescription", "autoWidth": true },
{ "title": "Last Booked Clinic Category Code", "data": "LastBookedClinicCategoryCode", "autoWidth": true },
{ "title": "Last Booked Clinic Category", "data": "LastBookedClinicCategory", "autoWidth": true },
{ "title": "Last Booked Clinic Code", "data": "LastBookedClinicCode", "autoWidth": true },
{ "title": "Last Booked Clinic Description", "data": "LastBookedClinicDescription", "autoWidth": true },
{ "title": "Mobile No", "data": "patient.MobileNumber", "autoWidth": true },
{ "title": "Overwrite Mobile No", "data": null },
{ "title": "Status", "data": "Status", "autoWidth": true }
],
"select": {
"style": 'os',
"selector": 'td:first-child'
}
});
</script>
<div class="form-group">
</div>
<div>
<ul class="nav nav-tabs" role="tablist" id="DetailsTab">
<li role="presentation" class="active"><a href="#Referral" aria-controls="Referral" role="tab" data-toggle="tab" class="tbs">Referrals</a></li>
<li role="presentation"><a href="#Responses" aria-controls="Response" role="tab" data-toggle="tab" class="tbs">Responses</a></li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="Referral">
<br />
</div>
<div>
<table class="table table-striped" id="example"></table>
</div>
</div>
</div>
}
--Code for post method--
$('#sendSMS').click(function () {
var formData = $('#sendSMSForm').serialize();
$.ajax({
type: "POST",
url: "/SMS/SendSMSForClients",
data: formData, //if you need to post Model data, use this
success: function (result) {
$("#partial").html("");
$("#partial").html(result);
searchReferrals(referralSpecialty, referralClinicName, referralClinicCode, referralPriority, referralStartDate, referralEndDate);
$('.nav-tabs a[href="#patientsReferral"]').tab('show');
$('.tab-pane a[href="#patientsReferral"]').tab('show');
$("#loading").hide();
},
error: function (jqXHR, textStatus, errorThrown) {
$("#partial").html("");
$('#partial').html('<p>status code: ' + jqXHR.status + '</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>' + jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
},
});
}