How to send a json object or array to mvc controller

3 comments


in js


			var sendData=new Array();
			for(var sc=1;sc<rows.length;sc++){//start on row 1, not zero
				sendData.push({
					index:sc,
					data:$($(rows[sc]).find('td')[modelsInThisCol-1]).html()});
			}
			//alert(msg);
			var url = websiteBaseUrl + 'admin/ProductionOrderDataAdmin/GetInfo';
			
			$.ajax( {
				type: "POST",
				url: url,
				dataType: 'json',
				//contentType: 'application/json; charset=utf-8',
				data: {'sendData':JSON.stringify( sendData)},
				success:function (result) {
					
				},
				error:function (msg) {
					alert( "error: " + msg.responseText );
					
				}
			});

in mvc


		[HttpPost]
		public ActionResult GetInfo(string sendData) {
			////"sendData[0][index]"
			//int numItems = sendData.AllKeys.Length;
			//for (int sc = 0; sc < numItems; sc++) {
			//	var keyName = sendData.AllKeys[sc];			//eg "sendData[0][index]"
			//	string data = sendData[keyName];
			//	if(keyName.Contains(""))
			//}

			var javaScriptSerializer = new JavaScriptSerializer();
			var data = javaScriptSerializer.Deserialize(sendData); return Content("ok"); } 


serialise in mvc


var javaScriptSerializer = new JavaScriptSerializer();
     var data = javaScriptSerializer.Serialize(obj);

Comments


Leave a Comment