Edit Image in database when field is empty [closed]

2

I have a code that uploads an image. In the database worked beautifully. Now I need to make a code that only changes the image when it is selected. That is, in my case, when I change a record, you must have to select the image field. Otherwise, this is without image. Here is the code for Controller , Action Edit :

    public ActionResult Edit(Funcionario funcionario)
    {

        if (ModelState.IsValid)
        {
            if (funcionario.File == null)
            {

                funcionario.Foto = funcionario.Foto;
                funcionario.Freguesia = funcionario.Freguesia;
                funcionario.Concelho = funcionario.Concelho;

                db.Entry(funcionario).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {

                byte[] data = new byte[funcionario.File.ContentLength];
                funcionario.File.InputStream.Read(data, 0, funcionario.File.ContentLength);
                funcionario.Foto = data;

                db.Entry(funcionario).State = EntityState.Modified;
                db.SaveChanges();

            }

            return RedirectToAction("Index");
        }

        return View(funcionario);
    }

Follow View Create :

<td>@Html.LabelFor(model => model.Foto)</td>
        <td>
            @Html.TextBoxFor(model => model.File, new { Type="file"})
            @Html.ValidationMessageFor(model => model.Foto)

        </td>

.....

    
asked by anonymous 25.09.2015 / 14:08

2 answers

1

There are several things wrong here:

funcionario.Foto = funcionario.Foto;
funcionario.Freguesia = funcionario.Freguesia;
funcionario.Concelho = funcionario.Concelho;

This assignment makes no sense.

All your code can be changed to the following:

[HttpPost]
public ActionResult Edit(Funcionario funcionario)
{
    if (ModelState.IsValid)
    {
        if (funcionario.File != null && funcionario.File.ContentLength > 0)
        {
            byte[] data = new byte[funcionario.File.ContentLength];
            funcionario.File.InputStream.Read(data, 0, funcionario.File.ContentLength);
            funcionario.Foto = data;
        }

        db.Entry(funcionario).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(funcionario);
}

Just setting your photo as an empty byte array is enough.

    
25.09.2015 / 16:17
0

That's how it works Thanks everyone, any suggestions?

[HttpPost]
    [Authorize(Roles = "SuperAdmin, Admin")]
    public ActionResult Edit(Funcionario funcionario)
    {
        if (ModelState.IsValid)
        {
            if (funcionario.File != null && funcionario.File.ContentLength > 0)
            {
                byte[] data = new byte[funcionario.File.ContentLength];
                funcionario.File.InputStream.Read(data, 0, funcionario.File.ContentLength);
                funcionario.Foto = data;

                db.Entry(funcionario).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {

                db.Entry(funcionario).State = EntityState.Modified;
                db.Entry(funcionario).Property(m => m.Foto).IsModified = false;
                db.SaveChanges();
            }


            return RedirectToAction("Index");
        }

        return View(funcionario);
    }
    
25.09.2015 / 22:43