As per this post , in order for ASP.NET MVC to bind to a model on post back, the name attributes of the form controls must match the model properties.
Now as per the jQueryDataTable documentation , what we could set the id of elemnts by using DT_RowId.
Is there a way we could add name attaribute to jQueryDataTable.
Data I am working with at the moment.
"data":[
"ID":10801,"Name":"SelectedContainers_C05","Description":null,"Tag":null,"Container_ID":10,"IsSelected":false,"DT_RowId":10801},
{"ID":10802,"Name":"SelectedContainers_C06","Description":null,"Tag":null,"Container_ID":10,"IsSelected":false,"DT_RowId":10802},
{"ID":10803,"Name":"SelectedContainers_C07","Description":null,"Tag":null,"Container_ID":9,"IsSelected":false,"DT_RowId":10803},
{"ID":10804,"Name":"SelectedContainers_C08","Description":null,"Tag":null,"Container_ID":10,"IsSelected":false,"DT_RowId":10804},
{"ID":10805,"Name":"SelectedContainers_C09","Description":null,"Tag":null,"Container_ID":10,"IsSelected":false,"DT_RowId":10805}
}
And this is my javascript to bind json data to View.
Dashboard.Helpers.elementExists('#ContainersTable', function () {
$(this).DataTable(
{
"ajax": {
"url": "/API/Loaddata",
"type": "GET",
"datatype": "json"
},
"columns": [
{
"data": "IsSelected",
"render": function (data, type, row) {
if (type === 'display') {
return '<input type="checkbox" class="editor-active">';
console.log(data);
}
return data;
},
"className": "dt-body-center"
// "autoWidth": true
},
{ "data": "Name", "autoWidth": true }
],
"rowCallback": function (row, data) {
// Set the checked state of the checkbox in the table
$('input.editor-active', row).prop('checked', data.IsSelected == 1);
}
}
);
$(this)
.removeClass('display')
.addClass('table table-striped table-bordered');
});
I tried setting the name on row call back,but it is not working.
"rowCallback": function (row, data) {
// Set the checked state of the checkbox in the table
$('input.editor-active', row).prop('checked', data.IsSelected == 1);
$('input.editor-active', row).prop('name', data.Id);
}
Update
I have this View Model which holds Enums, DateTime and collection of containers. Now when I click submit I need all of them posted .
I could use jQuery to go to individual element and then get there value and post them back. But if I could use ASP.NET MVC biding by adding name to container collection..
ViewModel
public class ReportViewModel
{
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public ESampleTime SampleTime { get; set; }
public Mesaurands Measurands { get; set; }
public IEnumerable<ContainersViewModel> Containers { get; set; }
}
public class ContainersViewModel
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Tag { get; set; }
public int? Container_ID { get; set; }
public bool IsSelected { get; set; }
public int DT_RowId { get; set; }
}
The controller method this will post back to is
[HttpPost]
public ActionResult Index(ReportViewModel vm)
{
....
}