here is my controller code ...
<pre lang="C#">[HttpPost]
public ActionResult Create(FormCollection collection,string command,List<Orderdetail> Values)
{
try
{
List<FILEDETAIL> FILEDETAILs = new List<FILEDETAIL>();
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
FILEDETAIL fILEDETAIL = new FILEDETAIL()
{
FileName = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid()
};
FILEDETAILs.Add(fILEDETAIL);
var path = Path.Combine(Server.MapPath("~/App_Data/Upload"), fILEDETAIL.Id + fILEDETAIL.Extension);
file.SaveAs(path);
}
}
List<REQ_ITEM_DETAILS> reqitem = new List<REQ_ITEM_DETAILS>();
for (int i=0;i<Values.Count;i++ )
{
REQ_ITEM_DETAILS req = new REQ_ITEM_DETAILS()
{
CO_COMP_CODE="0",
CO_REQ_ID=reqviewmodel.tBL_REQUISITION.CO_REQ_ID,
DESCRIPTION=Values[i].itemName,
QUANTITY=Values[i].quantity,
RATE=Values[i].rate,
TOTAL=Values[i].TotalAmount,
UNIT_ID=Convert.ToInt32(Values[i].unit)
};
reqitem.Add(req);
}
reqviewmodel.Addrequisition(collection, FILEDETAILs, Request.Files.Count,reqitem);
return RedirectToAction("Index");
}
catch(Exception ex)
{
return View();
}
}</pre>
on view i use this
<pre lang="JavaScript">$('#submit').click(function () {
var isAllValid = true;
var params = $('#f11').serializeArray();
if (isAllValid) {
var data = {
"command": "Insert",
"Values": orderItems,
"collection": params
}
$.ajax({
url: '/Requisition/Create',
type: "POST",
data: JSON.stringify(data),
dataType: "JSON",
contentType: "application/json",
success: function (d) {
if (d.status == true) {
alert('Record Save has been Successfully...');
}
else {
alert('Failed');
}
},
error: function () {
alert('Error. Please try again.');
}
});</pre>
What I have tried:
here is my controller code ...
[HttpPost]
public ActionResult Create(FormCollection collection,string command,List<Orderdetail> Values)
{
try
{
List<FILEDETAIL> FILEDETAILs = new List<FILEDETAIL>();
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
FILEDETAIL fILEDETAIL = new FILEDETAIL()
{
FileName = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid()
};
FILEDETAILs.Add(fILEDETAIL);
var path = Path.Combine(Server.MapPath("~/App_Data/Upload"), fILEDETAIL.Id + fILEDETAIL.Extension);
file.SaveAs(path);
}
}
List<REQ_ITEM_DETAILS> reqitem = new List<REQ_ITEM_DETAILS>();
for (int i=0;i<Values.Count;i++ )
{
REQ_ITEM_DETAILS req = new REQ_ITEM_DETAILS()
{
CO_COMP_CODE="0",
CO_REQ_ID=reqviewmodel.tBL_REQUISITION.CO_REQ_ID,
DESCRIPTION=Values[i].itemName,
QUANTITY=Values[i].quantity,
RATE=Values[i].rate,
TOTAL=Values[i].TotalAmount,
UNIT_ID=Convert.ToInt32(Values[i].unit)
};
reqitem.Add(req);
}
reqviewmodel.Addrequisition(collection, FILEDETAILs, Request.Files.Count,reqitem);
return RedirectToAction("Index");
}
catch(Exception ex)
{
return View();
}
}
on view i use this
$('#submit').click(function () {
var isAllValid = true;
var params = $('#f11').serializeArray();
if (isAllValid) {
var data = {
"command": "Insert",
"Values": orderItems,
"collection": params
}
$.ajax({
url: '/Requisition/Create',
type: "POST",
data: JSON.stringify(data),
dataType: "JSON",
contentType: "application/json",
success: function (d) {
if (d.status == true) {
alert('Record Save has been Successfully...');
}
else {
alert('Failed');
}
},
error: function () {
alert('Error. Please try again.');
}
});