1

在开发 ASP.NET MVC 4 应用程序时,我在更新保存的图像时遇到了问题。基本上,当用户选择编辑选项卡时,他们会重定向到他们之前输入的内容。该内容的一个这样的项目是图像,当在编辑菜单中时,他们可以选择替换现有图像。我正确更新了其他内容,但图像保持不变,当我在“编辑”ActionResult 上放置断点时,它会将传递模型的图像值显示为空?newsArticle.ArticleImage = 空

仅供参考 - 图像保存为字节数组。

我错过了什么吗?

控制器....

    [HttpPost]
    public ActionResult Edit(NewsArticle newsArticle, int id, HttpPostedFileBase Article)
    {
        try
        {
            if (ModelState.IsValid)
            {


             NewsArticle savedArticle= _newsArticle.Get(id);

             savedArticle.Body = newsArticle.Body;
             savedArticle.Title = newsArticle.Title;

             if (newsArticle.ArticleImage == null)
             {
                 newsArticle.ArticleImage = savedArticle.ArticleImage;
             }
             else
             {
                 using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
                 {
                     newsArticle.ArticleImage = binaryReader.ReadBytes(Request.Files[0].ContentLength);
                 }

                 savedArticle.ArticleImage = newsArticle.ArticleImage;
             }

             if (newsArticle.ImageName == null)
             {
                 newsArticle.ImageName = savedArticle.ImageName;
             }
             else
             {
                 string imgeName = Path.GetFileName(Article.FileName);

                 savedArticle.ImageName = imgeName;

             }

             _uow.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (System.Data.DataException)
        {
            //Log th error(add a variable name after DataExpection)
            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
        }

        return View(newsArticle);
    }

看法..........

         @using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", id = "newsEditForm" }))
{

    @Html.ValidationSummary()

    @Html.HiddenFor(model => model.ID)


    <div class="control-group">
        <label class="control-label">Posted on :</label>
        <div class="controls">
            <span class="text">@Model.DateCreated.Value.ToShortDateString()</span>
            @*@Html.LabelFor(n => n.DateCreated)*@
        </div>
    </div> 

    <div class="control-group">
        <label class="control-label">@Html.LabelFor(n => n.Title)</label>
        <div class="controls">

            @Html.TextBoxFor(n => n.Title, new { @class = "span4 m-wrap", rows = 1 })


        </div>
    </div> 

    <div class="control-group">
        <label class="control-label">@Html.LabelFor(n => n.Body)</label>
        <div class="controls">
            @Html.TextAreaFor(n => n.Body, new { @class = "span12 ckeditor m-wrap", rows = 4 })

        </div>
    </div> 

    <div class="control-group">
        <label class="controls">@Html.DisplayTextFor(model => model.ImageName)</label>
        <div class="span4 blog-img blog-tag-data">
            <div class="editor-field">
                <input type="file" name="Article" id="ArticleImage" />
            </div>
        </div>
    </div>

    <div class="form-actions">

        <button type="submit" class="btn green" id="submitnews"><i class="icon-ok"></i>Submit</button>
        @Html.ActionLink("Cancel", "ArticleList", "Admin", null, new { @class = "btn blue" })
        @*<button type="button" class="btn blue" onclick="location.href='ArticleList','Admin'">Cancel</button>*@

    </div>                   
}

4

0 回答 0