Basic help with mvc 3.0 and C#
Basic help with mvc 3.0 and C#
I want to be able to use the DataTable in my MVC project. I am having trouble finding a simple example.
I have my view:
Index
ID
Company name
Address
Town
I have my model:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Town { get; set; }
}
COntroller: THe code I saw is way to complex for me.
I want to populate say an array- an object with the data from the database and set the datatable.
Just basics without the datatable doing anything fancy.
What kind of object do I populate, how and how to connect to the datatable.
THank you for any help.
I have my view:
Index
ID
Company name
Address
Town
I have my model:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Town { get; set; }
}
COntroller: THe code I saw is way to complex for me.
I want to populate say an array- an object with the data from the database and set the datatable.
Just basics without the datatable doing anything fancy.
What kind of object do I populate, how and how to connect to the datatable.
THank you for any help.
This discussion has been closed.
Replies
If you go the JSON route, one approach that has worked for me is to just return a string (no ActionResult), and grab the string via an ajax call. Here's soething I used:
[code]
var oTable = $('#example').dataTable({
"sDom": 'C<"clear">Rlfrtip',
"bProcessing": true,
"aaSorting": [],
"sAjaxSource": "/Home/GetData",
"fnServerData": function (sSource, aoData, fnCallback) {
// Add some data to send to the source, and send as 'POST'
// aoData.push( { "name": "my_field", "value": "my_value" } );
$.ajax({
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
[/code]
The 'bProcessing' just provides an activity indicator when something changes, which is good if you're dealing with a lot of data. The 'aaSorting' allows you to specify an initial sort. Mine is empty to prevent any initial sorting, as this is done on the server. 'sAjaxSource' is the url to the method that will fill your table. This gets used in the following ajax function when the url is assigned 'sSource'. Don't ask me why - I'm really new to datatables and don't really know. Using 'aoData', you can send params to the server method. I've left in a commented-out example I took from the site. My table looks like this:
[code]
ID
Name
Create Date
Start Est
Start Actual
End Est
End Actual
Last Updated
Managers
Last Comment
Total Hours
Loading data from server
[/code]
Can you please post your method that loads from the database?"/Home/GetData
IN mVC what object do you save it too? I saw you shouldnt use datasets.
Or lets say for testing I would create an array and just populate it with fields- even hardcode ot see this all working.
Thanks again.