How to call the same Actionresult more than 1 time to refresh the page?

Asked

Viewed 553 times

1

I need to call Actionresult each time I select a new record in the Dropdownlist calling only 1 time when loading the page.

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>

  • Could you explain your problem better? Unable to access Actionresult? Unable to return data?

  • The problem is that it is calling only 1 time when it loads the page.

  • When you change Select, does it only search 1 time? In a new attempt it does not search in Action!?

  • Yes, just call once, I want to call whenever I select new information

1 answer

1

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

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

Using the resource Inspecionar Elementoof Google Chrome for example, you may realize that your @Html.DropDownList when rendered turns: <select id="idFoto" name="idFoto">, your select at the event change keeps returning the same values.

$(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 Select change will be executed this code, can also withdraw the onchange="BuscarCaminho()", will no longer be necessary as the above code picks up directly from the Select.

  • I changed the question with your code, how you are getting the idFoto, I changed my controller where I have a function to return me the name of the image path. Only the image is not loading, the path of the photo would be "C: image.jpg"

  • @itasouza I think you have confused things, I will edit my answer.

Browser other questions tagged

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