Codeone
Codeone

Reputation: 1201

Uploaded Image cant Edit in edit view

Im make The Image Uploaded Create Controller are really working and save the image for DB, Update are working but not replace image, alwyas display uplaoded image in grid view, please help me , how can i fix it? thanks

Create - Controller

  public ActionResult Save(Component component, HttpPostedFileBase file)
    {   
        string fileName = string.Empty;
        if (file != null && file.ContentLength > 0)
        {
            // extract only the fielname
            fileName = Path.GetFileName(file.FileName);
            // store the file12 inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/Uploaded"), fileName);
            file.SaveAs(path);
        }

        var objContext = new KnittingdbContext();
        component.CreateBy = 1;
        component.StatusChangeDate = System.DateTime.Now;
        component.CreatedDate = System.DateTime.Now;
        component.EditBy = 1;
        component.EditDate = System.DateTime.Now;
        //file name added here
        component.FileName = fileName;  
        objContext.Components.Add(component);
        objContext.SaveChanges();

        TempData["Success"] = "Saved Sucessfully";

        return RedirectToAction("ComponentIndex", new { A = "New" });
    } 






public ActionResult FileName(string id)
{
    byte[] image = { };

    try
    {
        image = System.IO.File.ReadAllBytes(Server.MapPath("~/Uploaded/") + id);
    }
    catch { }

    return File(image, "image/png");
}

My Update Controller:

 public ActionResult Update(Component component, HttpPostedFileBase file)
        { 
            string fileName = string.Empty;
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(file.FileName);
                // store the file12 inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Uploaded"), fileName);
                file.SaveAs(path);


                var objContext = new KnittingdbContext();
                //blog.Id = 1;
                objContext.Components.Attach(component);
                var obJBlog = objContext.Entry(component);

                obJBlog.Property(a => a.ComponentCode).IsModified = true;
                obJBlog.Property(a => a.ComponentName).IsModified = true;
                obJBlog.Property(a => a.Remarks).IsModified = true;
                obJBlog.Property(a => a.StatusId).IsModified = true;



                objContext.SaveChanges();

                TempData["SuccessEdit"] = "Saved Sucessfully";

                return RedirectToAction("ComponentIndex");
            }
            else
            {
                return PartialView("_ComponentEdit", component);
            }
        }

Update View

<div class="row" style="width:350px; margin-top:15px; margin-left:-10px;">
        @using (Html.BeginForm("Update", "Component", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {

            <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;"><b>Component Code</b><b style=" color:#ff0000;">*</b></div>

            <div class="col-md-6">
                @Html.TextBoxFor(a => a.ComponentCode, new { @maxlength = "5", Class = "form-control", style = "width:175px; height:25px;font-size:small;", onkeyup = "return validateChar(this)", @readonly = "readonly" })
                <div id="fname_error" style="margin-left:180px; width:140px;     top:5px; color:green; position:absolute; font-family:'Arial Unicode MS'; font-size:small;"></div>
                @Html.ValidationMessageFor(a => a.ComponentCode)
            </div>
            <div class="col-md-6"></div>    <div class="col-md-6"></div>
    <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;margin-top:5px;">
       <b> Component</b><b style=" color:#ff0000;">*</b>
    </div>
            <div class="col-md-6">
                @Html.TextBoxFor(a => a.ComponentName, new { @maxlength = "15", Class = "form-control", style = "width:175px; height:25px;margin-top:6px;font-size:small;" })
                @Html.ValidationMessageFor(a => a.ComponentName)
            </div>            <div class="col-md-6"></div>    <div class="col-md-6"></div>

        <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;">
           <b> Remarks</b>
        </div><div class="col-md-6"></div>    <div class="col-md-6"></div>
            <div class="col-md-6">
                @Html.TextBoxFor(a => a.Remarks, new { Class = "form-control", style = "width: 250px; height: 65px; resize: none;margin-top:4px;font-size:small;  " })
            </div>            <div class="col-md-6"></div>    <div class="col-md-6"></div>
    <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;">
        <b>Status</b>
    </div>

            <div class="col-md-6">
                @Html.CheckBoxFor(a => a.StatusId)
            </div>

            <div class="col-md-6">
                @Html.HiddenFor(a => a.ComponentId)
            </div>
            <div class="col-md-6"></div>    <div class="col-md-6"></div>
            <div class="col-md-6">
                <input type="submit" value="Update" class="btn btn-success" />
            </div>



    <div><input type="file" name="file" id="file" /> </div>


        }
    </div>

Upvotes: 1

Views: 1179

Answers (1)

lexeme
lexeme

Reputation: 2973

Probably because you're missing the line in your Update action:

component.FileName = fileName;

Here's the action:

public ActionResult Update(Component component, HttpPostedFileBase file)
{ 
    string fileName = string.Empty;

    if (file != null && file.ContentLength > 0)
    {
        // extract only the fielname
        fileName = Path.GetFileName(file.FileName);
        // store the file12 inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/Uploaded"), fileName);

        file.SaveAs(path);

        var objContext = new KnittingdbContext();
        // blog.Id = 1;

        // update the filename
        component.FileName = filename;

        objContext.Components.Attach(component);
        var obJBlog = objContext.Entry(component);

        obJBlog.Property(a => a.ComponentCode).IsModified = true;
        obJBlog.Property(a => a.ComponentName).IsModified = true;
        obJBlog.Property(a => a.Remarks).IsModified = true;
        obJBlog.Property(a => a.StatusId).IsModified = true;

        objContext.SaveChanges();

        TempData["SuccessEdit"] = "Saved Sucessfully";

        return RedirectToAction("ComponentIndex");
    }
    else
    {
        return PartialView("_ComponentEdit", component);
    }
}

Upvotes: 2

Related Questions