I have defined an object type in .NET that I want receive in a List<> as the input to an ASP.NET MVC action method?
Here is the action method and class I'm trying to receive.
public class WhereClause
{
public string ColumnInformation { get; set; }
public string WhereValue { get; set; }
public string AndOr { get; set; }
public string Comparer { get; set; }
}
public ActionResult Grid(string query, int skip = 0, int take = 50, List<WhereClause> whereClauses = null)
{
GridViewModel gvm = new GridViewModel();
gvm.Query = query;
And here is the Javascript where I'm building up the collection from a set of table rows using jQuery and then calling the jQuery ajax() method.
var whereClauses = [];
// Iterate over every row in the table and pull the values fromthe cells.
divQueryWidget.find('.tblWhereClauses tr').each(function (x, y) {
var tds = $(y).find('td');
var columnInformation = $(tds[0]).html();
var whereValue = $(tds[1]).html();
var andOr = $(tds[2]).html();
var comparer = $(tds[4]).html();
// Create a whereClause object
var whereClause = {};
whereClause.ColumnInformation = columnInformation;
whereClause.WhereValue = whereValue;
whereClause.AndOr = andOr;
whereClause.Comparer = comparer;
whereClauses.push({
ColumnInformation: columnInformation,
WhereValue: whereValue,
AndOr: andOr,
Comparer: comparer
});
});
//divQueryWidget.find('#queryResultsGrid').
$.ajax({
type: 'GET',
url: '<%= Url.Action("Grid", "Query") %>',
dataType: 'html',
data: { query: divQueryWidget.find('#activeQuery').val(), whereClauses: whereClauses },
success: function (data, textStatus, XMLHttpRequest) { divQueryWidget.find('#queryResultsGrid').append(data); divQueryWidget.find('.loading').css('visibility', 'hidden'); }
});
Here is where things get interesting. When the javascript is called and there were two rows in the table that are supposed to be passed to the MVC action, notice how when I debug into the code that there were two objects created in the list but their properties weren't filled.
What am I doing wrong that is preventing my Javascript object from being converted into a .NET List<> type? Should I be using array? Do I need to mark something as serializable?