这是我最后一次尝试,以获取关于此问题的一些见解,然后再共同尝试JqGrid。我的colModel中有一个col可上传图像。我正在使用ajaxfileupload。尽管有几个相同的例子,但它们似乎为其他人工作,而我的却不是。当我出于某种原因从enctype:“ multipart / form-data”中选择一个Image时,没有在对象的记录中放入任何内容。我有几个断点,当我查看数据时,除了产品Image以外,其他所有内容均可见。因此所有字段都在其中,ProductImage为null。就像JqGrid没有保存对象并传递它一样。由于为null,因此也不会上传任何内容。也许我错过了一些东西,但是我已经一遍又一遍地浏览了我的代码,它看起来就像我在假定有效的示例中阅读的内容一样。
任何帮助都是很棒的,因为我准备开始使用它并使用其他东西。
下面是我的程序代码。仅节。如果您需要查看其他代码,请告诉我。
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;
}
控制器:
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