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>
.....