How to use Server Side Processing and Drop Down Menus in ASP.Net MVC 2
How to use Server Side Processing and Drop Down Menus in ASP.Net MVC 2
XstreamINsanity
Posts: 23Questions: 0Answers: 0
I'm not sure if someone has already put this up here, but I want to put it up because I know many people who have had this issue and it does not seem to be easy to search for. Also, the examples on here only show PHP for the server side and don't explain how to show the controls when using server side. Hope you enjoy.
The view:
[code]
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
$(document).ready(function() {
$('#tableName').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/Controller/Function"
});
});
View
Column1
Column2
Column3
Column4
Column5
Column6
Column7
Column8
Column9
Column10
Loading data from Server
[/code]
The controller function:
[code]
public void Function()
{
//Get your data and save it appropriately...i.e. IEnumerable, DataSet, etc. We'll call it dataModel and in our example it's IEnumerable
IEnumerable dataModel = Repository.GetAllData();
//Make a variable of the same type as dataModel but it's empty, we'll call it newDataModel.
IEnumerable newDataModel;
System.Collections.Specialized.NameValueCollection nvc = HttpContext.Request.QueryString;
int nDisplayStart = Convert.ToInt32(nvc.Get("iDisplayStart"));
int nDisplayLength = Convert.ToInt32(nvc.Get("iDisplayLength"));
newDataMode = dataModel.Skip(nDisplayStart).Take(nDisplayLength).ToList();
string strOutput = "{";
strOutput += "\"sEcho\":" + nvc.Get("sEcho") + ", ";
strOutput += "\"iTotalRecords\":" + dataModel.Count().ToString() + ", ";
strOutput += "\"iTotalDisplayRecords\":" + dataModel.Count().ToString() + ", ";
strOutput += "\"aaData\": [";
foreach (class cl in newDataModel)
{
//Here, you'd iterate through each class, specifying the columns as they need to be. Below is an example of a project I'm working on, without giving out details.
strOutput += "[ ";
strOutput += "\"" + cl.Column1 + "\"";
strOutput += "\"" + cl.Column2 + "\"";
strOutput += "\"" + cl.Column3 + "\"";
strOutput += "\"" + cl.Column4 + "\"";
strOutput += "\"" + cl.Column5 + "\"";
strOutput += "\"" + cl.Column6 + "\"";
//Here are the drop down menus
string strSelectList = "\"--Select List Item--";
foreach (SelectListItem sli in Functions.GetListOfSelectListItems(parameters))
{
strSelectList += "" + sli.Text + "";
}
strOutput += strSelectList + "\",";
strSelectList = "\"--Select List Item--";
foreach (SelectListItem sli in Functions.GetListOfSelectListItems(parameters))
{
strSelectList += "" + sli.Text + "";
}
strOutput += strSelectList + "\",";
strSelectList = "\"--Select List Item--";
foreach (SelectListItem sli in Functions.GetListOfSelectListItems(parameters))
{
strSelectList += "" + sli.Text + "";
}
strOutput += strSelectList + "\",";
strOutput += "\"" + cl.Column10 + "\"";
}
strOutput += "]}";
Response.Write(strOutput);
}
[/code]
You could probably just do Response.Write instead of putting everything to a string, I just did so because I wanted to make sure that my output looks good before I throw it out there. When putting the code into a production environment, there is some processing lost to putting the information to a string. Please keep that in mind. Hope this helps people as it's helpt me. Thanks.
The view:
[code]
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
$(document).ready(function() {
$('#tableName').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/Controller/Function"
});
});
View
Column1
Column2
Column3
Column4
Column5
Column6
Column7
Column8
Column9
Column10
Loading data from Server
[/code]
The controller function:
[code]
public void Function()
{
//Get your data and save it appropriately...i.e. IEnumerable, DataSet, etc. We'll call it dataModel and in our example it's IEnumerable
IEnumerable dataModel = Repository.GetAllData();
//Make a variable of the same type as dataModel but it's empty, we'll call it newDataModel.
IEnumerable newDataModel;
System.Collections.Specialized.NameValueCollection nvc = HttpContext.Request.QueryString;
int nDisplayStart = Convert.ToInt32(nvc.Get("iDisplayStart"));
int nDisplayLength = Convert.ToInt32(nvc.Get("iDisplayLength"));
newDataMode = dataModel.Skip(nDisplayStart).Take(nDisplayLength).ToList();
string strOutput = "{";
strOutput += "\"sEcho\":" + nvc.Get("sEcho") + ", ";
strOutput += "\"iTotalRecords\":" + dataModel.Count().ToString() + ", ";
strOutput += "\"iTotalDisplayRecords\":" + dataModel.Count().ToString() + ", ";
strOutput += "\"aaData\": [";
foreach (class cl in newDataModel)
{
//Here, you'd iterate through each class, specifying the columns as they need to be. Below is an example of a project I'm working on, without giving out details.
strOutput += "[ ";
strOutput += "\"" + cl.Column1 + "\"";
strOutput += "\"" + cl.Column2 + "\"";
strOutput += "\"" + cl.Column3 + "\"";
strOutput += "\"" + cl.Column4 + "\"";
strOutput += "\"" + cl.Column5 + "\"";
strOutput += "\"" + cl.Column6 + "\"";
//Here are the drop down menus
string strSelectList = "\"--Select List Item--";
foreach (SelectListItem sli in Functions.GetListOfSelectListItems(parameters))
{
strSelectList += "" + sli.Text + "";
}
strOutput += strSelectList + "\",";
strSelectList = "\"--Select List Item--";
foreach (SelectListItem sli in Functions.GetListOfSelectListItems(parameters))
{
strSelectList += "" + sli.Text + "";
}
strOutput += strSelectList + "\",";
strSelectList = "\"--Select List Item--";
foreach (SelectListItem sli in Functions.GetListOfSelectListItems(parameters))
{
strSelectList += "" + sli.Text + "";
}
strOutput += strSelectList + "\",";
strOutput += "\"" + cl.Column10 + "\"";
}
strOutput += "]}";
Response.Write(strOutput);
}
[/code]
You could probably just do Response.Write instead of putting everything to a string, I just did so because I wanted to make sure that my output looks good before I throw it out there. When putting the code into a production environment, there is some processing lost to putting the information to a string. Please keep that in mind. Hope this helps people as it's helpt me. Thanks.
This discussion has been closed.
Replies
Thanks for posting this - I'm sure others will find it useful. There are a number of other server-side scripts which are available for DataTables here: http://datatables.net/development/server-side/
Allan
lol.
Yep, I'm doing C#/ASPX stuff too - though not MVC. Pagination is my next port of call...
ruzz