Jqgrid上传图像没有将数据对象传递给数据库 [英] Jqgrid upload image not passing data object to database

查看:119
本文介绍了Jqgrid上传图像没有将数据对象传递给数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我将JqGrid全部划分到一起之前,这是我最后一次尝试对此问题有所了解。我在colModel中有一个col来上传一个Image。我正在使用ajaxfileupload。虽然有几个相同的例子,但它们似乎适用于其他人,但我的不是。当我从enctype中选择一个Image:multipart / form-data由于某种原因它没有在对象的记录中放入任何东西。我有几个断点,当我查看数据时,一切都是可见的,但产品图像。所以所有字段都在那里,ProductImage为null。就像JqGrid没有持有对象并传递它一样。由于它为null,因此也不会上传任何内容。也许我错过了一些东西,但我一遍又一遍地查看我的代码,它看起来就像我在可能有效的例子中所读到的那样。





任何帮助都会很棒,因为我已经准备好抓住这个并使用别的东西了。



以下是我的流程代码。只有部分。如果您需要查看其他代码,请告诉我。



我尝试过的事情:



JqGrid:

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.

What I have tried:

JqGrid:

                {
                    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:

        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

推荐答案

.parseJSON(response.responseText);
// if (json)return [json.success,json.message,json.id];
// return [false,无法从服务器获得结果。,null];
var data =
.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
.parseJSON(response.responseText); if (data.success == true) { if (


#ProductImage)。val()!= ){
ajaxFileUpload(data.id);
}
}

return [data.success,data.message,data.id];
}

function ajaxFileUpload(id){
("#ProductImage").val() != "") { ajaxFileUpload(data.id); } } return [data.success, data.message, data.id]; } function ajaxFileUpload(id) {


这篇关于Jqgrid上传图像没有将数据对象传递给数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆