I am trying to pass strings array to my mvc action but I am getting Null value.
This is my javascript/Jquery
var array = ["Good Morning", "Good Afternoon", "Good Evening"]
var postData= { imageList: array };
$('button').on('click', function () {
$.ajax({
url: "/Home/Greetings",
type: 'POST',
data: postData,
dataType: "json",
traditional: true,
success: function (data) {
array = [];
},
error: function (xhr, status, err) {
}
});
});
My Code in Asp.net MVC is
[HttpPost]
public string Greetings(List postData)
{
string result = "";
foreach(var item in postData)
{
result += item + " ";
}
return result;
}

Ramesh PalanivelRank #372 · 6.8K reputationPosted Feb 18, 2018, 6:08 PM EST
Istudent RanaRank #4,270 · 354 reputationPosted Feb 19, 2018, 2:30 AM EST
I tried the way you asked me, I got below error:
Unexpected token P in JSON at position 0
Istudent RanaRank #4,270 · 354 reputationPosted Feb 19, 2018, 2:40 AM EST
SyntaxError: Unexpected end of JSON input
at parse (
Istudent RanaRank #4,270 · 354 reputationPosted Feb 19, 2018, 2:54 AM EST
// var imagename = imageID.substring(2);
// var image = "";
// if (typeof newimagename != "undefined") {
// if (newimagename.length != 0) {
// image = newimagename;
// }
// else {
// image = imagename;
// }
// }
// else {
// image = imagename;
// }
// var day = $("#questionlist").attr("data-day");
// var StoreNumber = $("#questionlist").attr("data-StoreNumber");
// var ProjectID = $("#questionlist").attr("data-ProjectID");
// var name = image.replace(/\./g, '\\.');
// imagename = imagename.replace(/\./g, '\\.');
// if (jpg == -1) {
// image = image + ".jpg";
// }
imgSourcePathCollection = ["1615\\1211\\0\\audi.jpg", "1615\\1211\\0\\bmw.jpg", "1615\\1211\\0\\mustang.jpg"]
url: "/Home/DownloadImages",
headers: { "__RequestVerificationToken": token },
type: 'POST',
data: JSON.stringify(postData),
dataType: "json",
traditional: true,
success: function (data) {
imgSourcePathCollection = [];
},
[HttpPost]
public void DownloadImages(List
{
try
{
string imgRootPath = ConfigurationManager.AppSettings["filepath"] + "\\";
string imgKey = string.Empty;
{
string[] fileNames = postData.FirstOrDefault().ToString().ToLower().Split('\\');
string fileName = "DaysImage_";
fileName += fileNames[0] + fileNames[1] + fileNames[2] + ".zip";
{
foreach (var imgPath in postData)
{
imgKey = imgPath.Substring(imgPath.LastIndexOf("\\") + 1);
imgFile = imgRootPath + imgPath;
zipFile.AddEntry(imgKey, imgFile);
}
Response.ClearHeaders();
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
}
}
catch (Exception ex)
{
string s = ex.ToString();
}
}
Istudent RanaRank #4,270 · 354 reputationPosted Feb 19, 2018, 2:57 AM EST
Mangesh GRank #294 · 8.6K reputationPosted Feb 19, 2018, 3:14 AM EST
Ramesh PalanivelRank #372 · 6.8K reputationPosted Feb 19, 2018, 3:27 AM EST
Istudent RanaRank #4,270 · 354 reputationPosted Feb 19, 2018, 3:28 AM EST
Istudent RanaRank #4,270 · 354 reputationPosted Feb 19, 2018, 3:45 AM EST
{
static List
{
"1615\\1211\\0\\mustang.jpg",
"1615\\1211\\0\\audi.jpg",
"1615\\1211\\0\\bmw.jpg"
};
public ActionResult Index()
{
DownLoadZip(ingList);
return View();
}
{
string imgFile = "";
string imgKey = "";
if (imgList.Count > 0)
{
string[] fileNames = imgList.FirstOrDefault().ToString().ToLower().Split('\\');
string fileName = "DaysImage_";
fileName += fileNames[0] + fileNames[1] + fileNames[2] + ".zip";
{
{
imgKey = imgPath.Substring(imgPath.LastIndexOf("\\") + 1);
imgFile = imgRootPath + imgPath;
zipFile.AddEntry(imgKey, imgFile);
}
Response.ClearHeaders();
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName );
}
}
}
Dharmraj ThakurRank #577 · 4.2K reputationPosted Feb 18, 2018, 6:30 PM EST
Dharmraj ThakurRank #577 · 4.2K reputationPosted Feb 19, 2018, 2:51 AM EST
Dharmraj ThakurRank #577 · 4.2K reputationPosted Feb 19, 2018, 3:16 AM EST