I am trying to edit records of a catalog, this catalog has the option to upload an image but it is not a required field. The problem is that when editing the record, whether that record already has an image saved or not, it sends me an error if I don't select an image from the input, it's as if it were a required field.
The error is this...
Object reference not set to an instance of an object. Image was null.
I am supposed to call the image that is stored on the server but I don't know how to do it.
Here is what I have in the Controller Building...
public ActionResult Update(int id)
{
if (Session["Usuario"] == null)
{
return RedirectToAction("Index", "Login");
}
else
{
ObtenerEdificio resp = obj.catEdificioObtener(id);
return View(resp);
}
}
[HttpPost]
public ActionResult Update(ObtenerEdificio vm, HttpPostedFileBase Imagen)
{
byte[] fileBytes = new byte[Imagen.InputStream.Length];
int byteCount = Imagen.InputStream.Read(fileBytes, 0, (int)Imagen.InputStream.Length);
string fileContent = Convert.ToBase64String(fileBytes);
ActualizaEdificio entrada = new ActualizaEdificio();
entrada.IdEdificio = vm.IdEdificio;
entrada.Descripcion = vm.Descripcion;
entrada.Imagen = fileContent;
RespuestaGeneral resp = obj.catEdificioActualiza(entrada);
ViewBag.successMessage = resp.Mensaje;
return RedirectToAction("Index", "Edificios");
}
Here is what I have in my View Update...
@using (Html.BeginForm("Update", "Edificios", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.HiddenFor(d => d.IdEdificio)
<div class="box-body">
<div class="form-group">
<label for="inputDescripcion" class="form-control-label mt-3 ml-5">Nombre:</label>
@Html.ValidationMessageFor(m => m.Descripcion, "", new { @class = "ml-2", style = "color:red" })
@Html.TextBoxFor(m => m.Descripcion, "", new { @type = "text", @class = "form-control ml-5", style = "width:92%" })
</div>
<div class="form-group" style="text-align:center">
<img class="mb-3" style="width:150px; height:150px;" onerror="this.src='/ImagenesSubidas/SinImagen.jpg';" src="@Url.Content(@Model.Imagen)" id="preview" />
<div class="box-footer">
<label for="file-upload" class="subir">
<i class="fas fa-cloud-upload-alt"></i> Subir archivo
</label>
<input type="file" accept="image/*" class="block" onchange='cambiar()' style='display:none;' id="file-upload" name="Imagen" />
<div id="info"></div>
</div>
</div>
</div>
<div class="box-footer" style="text-align:center">
<button type="submit" class="btn block btn-success mt-3 mb-3" style="width: 30%" name="crear">Guardar <i class="fa fa-plus-circle"></i></button>
</div>
}
I hope you understand me and can help me with my problem. Thanks.
Imagen
The error is because the method parameterUpdate
can arrive asnull
since image is optional.If the value of
Imagen
isnull
,Imagen.CualquierPropiedad
it will give an error.It would be necessary to do some type of validation to this parameter in case it is null, skipping the part of the code that refers to it.
Or work with the operator
??
.At last I managed to do it and here I leave the code that I added in my Controller, in case someone finds it useful...