How do I call the same ActionResult more than 1 time to refresh the page?

1

I need to call ActionResult each time I select a new record in the DropDownList by calling only once when the page loads.

No Controller

        private string PegarCaminhoImagem(Int16 controle)
    {
        sistema_mobileEntities dao = new sistema_mobileEntities();
        dao.fotos.Find(controle);
        var caminho = dao.fotos.First().Foto;
        return caminho;
    }


    public ActionResult AtualizaFoto(Int16 caminhofoto)
    {
        int largura = 100; 
        int altura = 100;
        String CaminhoFoto = "";

        try
        {

           CaminhoFoto = PegarCaminhoImagem(caminhofoto);

           var webImagem = new WebImage(@CaminhoFoto).Resize(largura, altura, false, false);
           return File(webImagem.GetBytes(), @CaminhoFoto);

        }
        catch (Exception ex)
        {
            return Json("A Imagem não existe : " + ex.Message);
        }


    }

Na Views:

@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("/ConsultaCliente/AtualizaFoto", { caminhofoto: srcRecebe }).done(function (data) {
                    $('#caminho').attr("src", data);
                })
            }
        });
    });

</script>

               

<form>
    <fieldset data-role="controlgroup">


           <label>Foto:</label>
           <div id="selecao">
                 @Html.DropDownList("idFoto", String.Empty) 
           </div>


           <div >
             <img id="foto" src="@Url.Action("AtualizaFoto", "ConsultaCliente", new {caminhofoto = "caminho" })" 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>
    
asked by anonymous 18.04.2015 / 01:01

1 answer

1

You can change your code to get the value of Select as follows:

When you use HTML Helper : @Html.DropDownList("idFoto", String.Empty) the "idFoto" parameter becomes the value of the id and name attributes when it is rendered to HTML.

Using the select feature of Inspecionar Elemento for example, you may notice that your Google Chrome when rendered becomes: @Html.DropDownList , its <select id="idFoto" name="idFoto"> in event select keeps returning the same values. p>

$(document).ready(function () {
    $("#idFoto").on("change", function () {

        var srcRecebe = $($(this)).val();

        if (srcRecebe > 0) {

            $.post("/ConsultaCliente/AtualizaFoto", { caminhofoto: srcRecebe }).done(function (data) {
                $('#caminho').attr("src", data);
            })
        }
    });
});

Doing this, every time% change is executed, you can also remove change , it will not be necessary anymore, since the above code takes directly from Select .

    
20.04.2015 / 21:49