1
I have a code that uploads image. In the database worked beautifully. Now I need to make a code that changes the image only when it is selected. That is, in my case, when I change a record, you must select the image field. Otherwise, it is without image. Follows the code of 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);
}
Follows View Create
:
<td>@Html.LabelFor(model => model.Foto)</td>
<td>
@Html.TextBoxFor(model => model.File, new { Type="file"})
@Html.ValidationMessageFor(model => model.Foto)
</td>
.....
Could you clarify a little more? What is this field you need to select? Explain a little more
– Randrade
My problem is in this section there if I do not select a new image to edit that it recoils the image that is in the bd: if (funcio.File == null) funcionario. Photo = staff.Photo;
– Evandro barros