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

XstreamINsanityXstreamINsanity Posts: 23Questions: 0Answers: 0
edited August 2010 in General
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.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi XstreamINsanity,

    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
  • XstreamINsanityXstreamINsanity Posts: 23Questions: 0Answers: 0
    You're welcome. I see so many things online for VB I had to boost up C# some. :) Also, I didn't see those other scripts, so thank you for the link. I'm actually in the middle of writing out exactly what you sent me, and it looks rather similar (except for business rules here and there). My biggest concern, and reason, for making this post was moreso for the drop down menu and other controls. It was very difficult to find examples online for it for some reason. Thank you for a wonderful plugin. My co-worker and I were having an issue with too many records and we had other options such as regular pagination, but nothing was as appealing as this plugin with the auto search, sort (multi-column rather than my own one column sort), pagination and wonderful CSS style. (Yes, I agree with another post, the CSS does need a little tidying, lol). But we spent a day trying to research the best way to still use the dataTable and not lose any of the functionality. Kudos to you sir.
  • ruzzruzz Posts: 49Questions: 0Answers: 0
    Hey Xstream[...]

    lol.

    Yep, I'm doing C#/ASPX stuff too - though not MVC. Pagination is my next port of call...

    ruzz
This discussion has been closed.