This is a last attempt for me to get some insight on this issue before I scratch JqGrid all together. I have a col in my colModel to upload an Image. I am using ajaxfileupload. Although there are several of the same examples out there they seem to work for others but mine is not. When I select an Image from the enctype: “multipart/form-data” for some reason it is not putting anything in the record for the object. I have a few breakpoints and when I view the data everything is visible but the product Image. So all fields are there and ProductImage is null. It is like JqGrid is not holding the object and passing it. Since it is null, nothing gets uploaded either. Maybe I am missing something but I have looked through my code over and over again and it appears to be just like what I have read in examples that supposedly work.
Any help with would be fantastic as I am ready to scratch this and use something else.
Below is my code for the process. sections only. if you need to see other code let me know.
JqGrid:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
{ name: "ProductImage", index: "ProductImage", mtype: "Post", editable: true, editrules: { required: true }, edittype: "file", search: true, resizable: false, width: 210, align: "left", editoptions: { enctype: "multipart/form-data" } }, { // edit option zIndex: 100, url: "/Admin/EditProducts", closeOnEscape: true, closeAfterEdit: true, recreateForm: true, afterSubmit: uploadImage, afterComplete: function (response) { if (response.responseText) { alert(response.responseText); } } }, function uploadImage(response, postdata) { //debugger; //var json = $.parseJSON(response.responseText); //if (json) return [json.success, json.message, json.id]; //return [false, "Failed to get result from server.", null]; var data = $.parseJSON(response.responseText); if (data.success == true) { if ($("#ProductImage").val() != "") { ajaxFileUpload(data.id); } } return [data.success, data.message, data.id]; } function ajaxFileUpload(id) { $.ajaxFileUpload( { url: "/Admin/UploadImage", secureuri: false, fileElementId: "ProductImage", dataType: "json", data: { id: id }, success: function (data, status) { if (typeof (data.isUploaded) != "undefined") { if (data.isUploaded == true) { return; } else { alert(data.message); } } else { return alert("Failed to upload image!"); } }, error: function (data, status, e) { return alert("Failed to upload image!"); } } ) return false; } |
Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public string EditProducts(Product product) { string msg; try { if (ModelState.IsValid) { using (StoreEntities db = new StoreEntities()) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); msg = "Saved Successfully"; } } else { msg = "Did not save! "; } } catch (DbEntityValidationException ex) //catch (Exception ex) { msg = "Error occured:" + ex.Message; foreach (var validationErrors in ex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Diagnostics.Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } return msg; } .... #region Upload Images [HttpPost] public JsonResult UploadImage(HttpPostedFileBase ProductImage) { string directory = "~/Images/"; if (ProductImage != null && ProductImage.ContentLength > 0) { var fileName = Path.GetFileName(ProductImage.FileName); ProductImage.SaveAs(Server.MapPath(Path.Combine(directory, fileName))); //ProductImage.SaveAs(Path.Combine(directory, fileName)); } return Json(new { isUploaded = true, message = "Uploaded Successfully" }, "text/html"); } #endregion |
Copyright 2014 TriRand LtdAll Rights ReservedRSS
Back to Top