JSON.stringify(oTable.fnGetData()) post null array values in asp.net mvc3 action method.

JSON.stringify(oTable.fnGetData()) post null array values in asp.net mvc3 action method.

sunderjisunderji Posts: 1Questions: 0Answers: 0
edited November 2011 in General
Hi I want to send jQuery Data Table to Action Method in asp.net MVC3. Though it display data in alert method before ajax call. It send null rows in action method collection.

What is the problem ?

Thanks in advance.
Sham Sunder


ViewPage
--------------------------------------------------
$(document).ready(function () {

$('#example').dataTable();
oTable = $('#example').dataTable();

$('#btnAdd').click(function () {
$('#example').dataTable().fnAddData([
$('#txtSrNo').val(),
$('#txtProductName').val(),
$('#txtQuantity').val(),
$('#txtRate').val(),
$('#txtQuantity').val() * $('#txtRate').val()
]);
});

$('#btnSave').click(function (e) {
sData = oTable.fnGetData();
alert(JSON.stringify(sData));
var data = { OrderItems: sData };
$.ajax({
url: "/Product/Create",
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(data),
success: function (result) {
alert("yes " + result.toString());
},
error: function (result) {
alert("no " + result.toString());
}
});

Action Method
-------------------------------------------------------
[HttpPost]
public ActionResult Create(OrderViewModel collection)
{
return View();
}

Model
---------------------------------------------------------
namespace MasterDetailTest.ViewModels
{
public class OrderItem
{
public int Srno { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public int Rate { get; set; }
public int Amount { get; set; }

public OrderItem() { }
}

public class OrderViewModel
{
public IEnumerable OrderItems { get; set; }

public OrderViewModel()
{
OrderItems = new List();
}
}
}

Replies

  • allanallan Posts: 63,552Questions: 1Answers: 10,477 Site admin
    I don't know anything about ASP.NET I'm afraid, but I'd suggest not creating a JSON string for the data and instead just passing it straight in. For example:

    [code]
    data: data,
    [/code]

    That should send the data through. Firebug / Inspector will confirm this.

    Allan
This discussion has been closed.