Array list data not come in controller

Posted by Shail under ASP.NET MVC on 8/24/2015 | Points: 10 | Views : 1296 | Status : [Member] | Replies : 1
this is javascript..........

<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">

function product() {

this.Id = "";
this.brand = "";
this.model = "";
this.image = "";
this.price = "";
this.stock = "";
this.offer = "";
this.website = "";
this.url = "";
}



jQuery.ajax({
type: "GET",
url: "http://api.pricecheckindia.com/feed/product/mobile_phones.json?user=headboy6&key=BNTLMKXUHBKQJGUY",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
crossOrigin: true,
async: true,
success: function (response) {

// console.log(response.product);

var dataToSend = new Array();
//var mydata = dataToSend;
$.each(response.product, function (index, Value) {

var p = new product();
p.Id = response.product[index].id;
p.brand = response.product[index].brand;
p.model = response.product[index].model;

var stores = response.product[index].stores;

$.each(stores, function (index, Value) {

if (index === 0) {
p.image = stores[index].image,
p.price = stores[index].price,
p.stock = stores[index].stock,
p.offer = stores[index].offer,
p.website = stores[index].website,
p.url = stores[index].url

}

})
dataToSend.push(p);
});


console.log(dataToSend);
Send(dataToSend);

},


error: function (msg) {
console.log(msg);
alert(msg)
}
});

function Send(d) {
var mydata = { product: d };
jQuery.ajax({
url: "/Home/Test",
type: "POST",
dataType: "json",
data: mydata,
//dataType: "xml",
// contentType: "application/json; charset=utf-8",
async: false,
cache: false,
success: function (data) {
alert(data);
console.log(product);
},
error: function (errMsg) {
alert(errMsg);
console.log(errMsg);
}
});
}


</script>



controller is
[HttpPost]
public ActionResult Test(string[] Product)
{
return View();
}
plz help me
thanks




Responses

Posted by: Sheonarayan on: 8/25/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
1
Down
It would have been easier to understand the code if you would have kept it in the CODE block. However as far as I understand you are trying to get the data of the Product object that you have formed in the JavaScript.

In this way, it may not work. Better to receive the data as JSON. Look at this example http://techfunda.com/Howto/asp-net-mvc/301/insert-record-using-web-api. You must have a Product model in the ASP.NET MVC whose properties should be similar to the property of JavaScript object.

Alternatively, you can use FormCollection as shown here http://techfunda.com/Howto/asp-net-mvc/216/access-form-data-into-controller-using-formcollection.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Shail, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response