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.
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();
}
}
}
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();
}
}
}
This discussion has been closed.
Replies
[code]
data: data,
[/code]
That should send the data through. Firebug / Inspector will confirm this.
Allan