How to work with the @Html.DropDownList?

2

Friends
After following the example I made the adjustment in the code, I am not able to send the information to ActionResult

@model ProjetoDelphiMobile.Models.cliente

@{
    ViewBag.Title = ""; 
}

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script><script>functionBuscarCaminho(){varsrcRecebe=document.getElementById("selecao").innerText;
        alert(srcRecebe);

        if (srcRecebe > 0) {

            $.post("/ConsultaCliente/AtualizaFoto", { caminhofoto: srcRecebe }).done(function (data) {
                alert(data)
                window.location.reload();
            })
        }

    }

</script>

               

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


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


           <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>

In my Views:

        public ActionResult AtualizaFoto(string caminhofoto)
    {
        int largura = 100;
        int altura  = 100;
        WebImage webImagem = new WebImage(@caminhofoto).Resize(largura, altura, false, false);
        return File(webImagem.GetBytes(), @caminhofoto);
    }
    
asked by anonymous 14.04.2015 / 22:17

1 answer

2

In jQuery, your function would look like this:

<script >

        function BuscarCaminho()
        {
            //Pega o elemento 'select'
            var select = $("#idFoto");
            //Altera o valor do atributo 'src' da imagem para carregar a imagem selecionada
            if (select.length > 0) {
                $('#caminho').attr("src", select.val());
                alert(select.val());
            }
        }

</script>

There are a few more adjustments that will be needed. In Controller , for example, return a FileResult instead of an ActionResult:

public FileResult AtualizaFoto(string caminhofoto)
{
    int largura = 100;
    int altura  = 100;
    var webImagem = new WebImage(@caminhofoto).Resize(largura, altura, false, false);
    return File(webImagem.GetBytes(), @caminhofoto);
}

Another thing is this callback, which the way it is will not work. The right would be something like:

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