3
I have the following code:
public ActionResult AtualizaCliente(int id)
{
ViewBag.idFoto = new SelectList(dao.fotos, "idFoto", "Foto");
ViewBag.idInformacao = new SelectList(dao.informacoes, "idInformacao", "titulo");
return View(dao.cliente.Find(id));
}
private string PegarCaminhoImagem(Int16 controle)
{
sistema_mobileEntities dao = new sistema_mobileEntities();
dao.fotos.Find(controle);
var caminho = dao.fotos.First().Foto;
return caminho;
}
public WebImage ObterWebImage(short idFoto)
{
int largura = 100;
int altura = 100;
String caminhoFoto = PegarCaminhoImagem(idFoto);
return new WebImage(@caminhoFoto).Resize(largura, altura, false, false);
}
public ActionResult AtualizaFoto(Int16 caminhofoto)
{
try
{
WebImage webImagem = ObterWebImage(caminhofoto);
return File(webImagem.GetBytes(), "image/png");
}
catch (Exception ex)
{
return Json("A Imagem não existe : " + ex.Message);
}
}
public string ObterFotoBase64(Int16 caminhofoto)
{
try
{
WebImage webImagem = ObterWebImage(caminhofoto);
return Convert.ToBase64String(webImagem.GetBytes());
}
catch (Exception ex)
{
return "A Imagem não existe : " + ex.Message;
}
}
in the Controller.
How best to pass the image path to be viewed?
@model ProjetoDelphiMobile.Models.cliente
@{
ViewBag.Title = "";
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function () {
$("#idFoto").on("change", function () {
var srcRecebe = $($(this)).val();
if (srcRecebe > 0) {
$.post("@Url.Action("ObterFotoBase64", "ConsultaCliente")", { idFoto: srcRecebe }).done(function (data) {
alert(data);
$('#foto').attr("src", "data:image/image/png;base64," + data);
});
}
});
});
</script>
<form>
<fieldset data-role="controlgroup">
<label>Foto:</label>
<div id="selecao">
@Html.DropDownList("idFoto", String.Empty)
</div>
<br />
<div>
<img id="foto" src="@Url.Action( "AtualizaFoto", "ConsultaCliente", new {caminhofoto = 1 })" alt="thumbnail" />
</div>
<br />
<label>Nome:</label>
@Html.TextBoxFor(model => model.nome, new { disabled = false })
<label>Nome:</label>
@Html.TextBoxFor(model => model.nome, new { disabled = false })
<label>Nome do pai:</label>
@Html.TextBoxFor(model => model.pai, new { disabled = false })
<label>Nome da Mãe:</label>
@Html.TextBoxFor(model => model.mae, new { disabled = false })
<label>Data de Nascimento:</label>
@Html.TextBoxFor(model => model.datanascimento, new { disabled = false })
<ul data-role="listview" data-inset="true" data-divider-theme="e">
<li><a href="/ConsultaCliente">Retornar para consulta</a></li>
</ul>
</fieldset>
</form>
Apparently it worked, at the top of the page called the Updatephoto, but when selecting the Dropdownlist is not calling the Obtenfotobase64, I changed all the code to look like the one you passed, can help me?
– Harry
The event
change
dropdown is being fired? If so, I found a difference in the controller name in my code. I tested using a controller namedHomeController
, but your controller callsConsultaClienteController
. Then the line of$.post
should be$.post("@Url.Action("ObterFotoBase64", "ConsultaCliente")"
. Make sure your code is not being usedHomeController
as I was before in my reply.– Marcus Vinicius
In my question, I changed all the code according to the adjustment I made, $.post("@Url.Action("Get Photobase64", "Customer Consultation")",
– Harry
Is the dropdown change event being triggered? The method
ObterFotoBase64
controller is being called? Is it returning? jQuery crashed into the eventdone
? Please, you need to specify what is going wrong, do a debug and tell us.– Marcus Vinicius
The Get photobase64 method is not being called! thanks!
– Harry