Edit registered containing image

Asked

Viewed 71 times

0

I have my model:

public class Produto 
{
   public int Id {get;set;}
   public string Nome {get;set;}
   public byte[] Imagem {get;set;}
}

How do I handle it if it is "Removing" or is Editing(Replacing) the Product image? In my View:

  @Html.TextBoxFor(model => model.ImagemUpload, new { type = "file", @class = "filestyle form-control", @data_size = "sm" })

In my Viewmodel I have the property

public HttpPostedFileBase ImagemUpload{ get; set; }

Which I check on the controller

if(model.ImagemUpload!= null) 
{
   Tem Imagem então salva/Edita
}

Here in Edit, New, it works, but how do I know when it’s removing? Because if it is removing the model.Imagemupload will come Null, and I need to delete the value that is with Byte[] in my table.

1 answer

1

What I usually do in this case is a separate Ajax request the moment he clicks to remove the image.

Another alternative is as soon as he clicks to remove the image, Voce just deletes it from the screen and adds a <input type="hidden" name="imagem_excluida" value="true /> in HTML. That way, when the form is submitted, you check this property to see if there was an exclusion or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.