0
$("#btnSubmit").click(function() {
  debugger
  var Pid = $("#hdnPid").val();
  console.log(formdata);

  $("loader-area,.loader").css("display", "block");

  $.ajax({
    url: '@Url.Action("InsertUpdateProduct")',
    contentType: 'application/json; charset=utf-8',
    data: $('#formId').serialize(),
    type: 'POST',
    success: function(data) {
        debugger
        if (data[0].Retval = "1") {
          toastr.success('Record Saved Successfully.', 'Congratulations!');
        } else if (data[0].Retval = "2") {
          toastr.success('Record Updated Successfully.', 'Congratulations!');
        } else {
          toastr.warning('Exception Occured', 'Warning!');
        }
        ProductMaster();
        ClearData();
      }

      - List item
  });
});

Controller

[HttpPost]
public JsonResult InsertUpdateProduct(ProductMaster productMaster, IFormFile files) 
{
  List<ProductMaster> ObjProduct = new List<ProductMaster>();
  try 
  {
    if (ModelState.IsValid)   
    {
      if (files != null) 
      {
        if (files.Length > 0) 
        {
          //Getting FileName
          var fileName = Path.GetFileName(files.FileName);

          //Getting file Extension
          var fileExtension = Path.GetExtension(fileName);

          // concatenating  FileName + FileExtension
          var newFileName = String.Concat(Convert.ToString(Guid.NewGuid()), fileExtension);

          var objfiles = new ProductMaster() 
          {
            //DocumentId = 0,
            ImageName = newFileName,
            FileType = fileExtension,
            // CreatedOn = DateTime.Now
          };

          using(var target = new MemoryStream()) 
          {
            files.CopyTo(target);
            objfiles.Image = target.ToArray();
            productMaster.Image = objfiles.Image;
          }
        }
      }

      //productMaster.PID = PID;            
      objProductData.InsertUpdateProduct(productMaster);
      if (productMaster.PID == 0) 
      {
        _notyf.Success("Record Saved Successfully");
      } 
      else 
      {
        _notyf.Success("Record Updated Successfully");
      }
    } 
    else 
    {
      _notyf.Warning("Model false display");
    }
  } 
  catch (Exception ex) 
  {
    _notyf.Error("Error Occured");
  }
  return Json(ObjProduct);
}
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
  • From the code in your controller it looks like you're attempting to upload file data, as such you need to use a FormData object, not serialised form data. Try this: https://stackoverflow.com/a/37762290/519413 – Rory McCrossan Nov 30 '21 at 10:04

0 Answers0