保存在数据库中的图像无法更新

时间:2012-07-19 14:55:50

标签: database asp.net-mvc-3 image linq

我一直在谷歌搜索解决我的问题两天没有运气。 MVC3 .NET中的任何明星都可以提供帮助吗?

我正在尝试构建一个.NET MVC3应用程序来更新保存在数据库中的图像。

这是动作方法

[HttpPost]
    public ActionResult Edit(myImage img, HttpPostedFileBase imageFile)
    {
        //var img = (from imga in db.myImages
        //              where imga.imageID == id
        //            select imga).First();
        if (ModelState.IsValid)
        {
            if (img != null)
            {
                img.imageType = imageFile.ContentType;
                img.Data  = new byte[imageFile.ContentLength];
                imageFile.InputStream.Read(img.Data, 0, imageFile.ContentLength);
            }
            // save the product
            UpdateModel(img);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }
        else
        {
            // there is something wrong with the data values
            return View(img);
        }
    }

这是视图

@model JackLing.Models.myImage

   @{
       ViewBag.Title = "Edit";
       Layout = "~/Views/Shared/_Layout.cshtml";
   }

   <h2>Edit</h2>

   <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
   <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

   @using (Html.BeginForm("Edit", "Image",FormMethod.Post, new { enctype = "multipart/form-data" })){
       @Html.ValidationSummary(true)
       <fieldset>
           <legend>myImage</legend>
           @Html.EditorForModel();

           <div class="editor-label">Image</div>
           <div class="editor-field">
           @if (Model.Data != null)
           {
            <img src="@Url.Action("show", new { id = Model.imageID })" height="150" width="150" />
           }
           else { 
           @:None
           }
           </div>

           <p>
              <span>Choose a new file</span> <input type="file" name="imgFile"/>
           </p>

           <p>
               <input type="submit" value="Save" />
           </p>

       </fieldset>
   }

   <div>
       @Html.ActionLink("Back to List", "Index")
   </div>

当我运行应用程序时,它会抛出一条错误,指出“对象引用未设置为对象的实例。”

任何有关如何解决问题的建议都会受到关注!顺便说一句,创建和细节方法都是有效的。我认为它与数据绑定有关,但我不确定......我不知道如何修复它。


最后根据Eulerfx的建议解决了问题 这是工作行动。

[HttpPost]
        public ActionResult Edit(myImage img, HttpPostedFileBase imageFile)
        {
            myImage imgToSave = (from imga in db.myImages
                          where imga.imageID == img.imageID
                         select imga).First();



            if (ModelState.IsValid)
            {
                if (img != null)
                {
                    imgToSave.imageType = imageFile.ContentType;
                    var binaryReader = new BinaryReader(imageFile.InputStream);
                    imgToSave.Data = binaryReader.ReadBytes(imageFile.ContentLength);
                    binaryReader.Close();
                }


                TryUpdateModel(imgToSave);

                db.SubmitChanges();

                return RedirectToAction("Index");
            }
            else
            {
                // there is something wrong with the data values
                return View(img);
            }
        }

1 个答案:

答案 0 :(得分:0)

问题可能是HTML中文件输入字段的名称是imgeFile,MVC中文件参数的名称是imageFile。确保输入字段名称和操作方法参数名称匹配。

相关问题