ASP.NET MVC Jquery Image and Text Save
Posted on Wednesday, February 3, 2016
|
No Comments
$("#btnAdd").click(function (e) { e.preventDefault(); //$("#form1").submit(); var file1; if (document.getElementById("exampleInputFile1").files.length > 0) file1 = document.getElementById("exampleInputFile1").files[0]; var file2; if (document.getElementById("exampleInputFile2").files.length > 0) file2 = document.getElementById("exampleInputFile2").files[0]; var file3; if (document.getElementById("exampleInputFile3").files.length > 0) file3 = document.getElementById("exampleInputFile3").files[0]; var file4; if (document.getElementById("exampleInputFile4").files.length > 0) file4 = document.getElementById("exampleInputFile4").files[0]; var file5; if (document.getElementById("exampleInputFile5").files.length > 0) file5 = document.getElementById("exampleInputFile5").files[0]; var formData = new FormData(); formData.append("MainGroupId", $("#MainGroup").val()); formData.append("MainGroupName", $("#MainGroup option:selected").text()); formData.append("SubGroupId", $("#SubGroup").val()); formData.append("SubGroupName", $("#SubGroup option:selected").text()); formData.append("NewBrandId", $("#NewBrandId").val()); formData.append("NewBrandName", $("#NewBrandId option:selected").text()); formData.append("NewCategoryId", $("#NewCategoryId").val()); formData.append("NewCategoryName", $("#NewCategoryId option:selected").text()); formData.append("NewColorId", $("#NewColorId").val()); formData.append("NewColorName", $("#NewColorId option:selected").text()); formData.append("IsActive", $("#IsActive").val()); formData.append("Description", $("#Description").val()); formData.append("Image1", file1); formData.append("Image2", file2); formData.append("Image3", file3); formData.append("Image4", file4); formData.append("Image5", file5); formData.append("OperationType", $("#hdnType").val()); $.ajax({ type: "POST", url: _urlBase + '/ProductManage/AddTempProduct', data: formData, dataType: 'json', contentType: false, processData: false, success: function (data) { if (data.ResultState == true) { // done } else if (data.ResultState == false) ShowMessage(data.SqlError); }, error: function (error) { alert("errror"); } }); });
Backend C# code
[HttpPost] public JsonResult AddTempProduct() { Result r = new Result(); try { ArticlePOCO article = new ArticlePOCO(); article.MainGroupId = Request.Form["MainGroupId"]; string OperationType = Request.Form["OperationType"]; if (Request.Files.Count > 0) { if (Request.Files["Image1"] != null) { article.Image1 = Request.Files["Image1"]; } if (Request.Files["Image2"] != null) { article.Image2 = Request.Files["Image2"]; } if (Request.Files["Image3"] != null) { article.Image3 = Request.Files["Image3"]; } if (Request.Files["Image4"] != null) { article.Image4 = Request.Files["Image4"]; } if (Request.Files["Image5"] != null) { article.Image5 = Request.Files["Image5"]; } } } catch (Exception ex) { r.ResultState = false; r.SqlError = ex.Message; } System.Web.Mvc.JsonResult jsonRe = new System.Web.Mvc.JsonResult() { Data = r, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.DenyGet }; return jsonRe; }