How to work with @Html.Dropdownlist?

Asked

Viewed 118 times

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>

    function BuscarCaminho()
    {
        var srcRecebe = 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);
    }
  • 1

    Isn’t it easier to use jQuery to do this? You put the tag on the question but you’re not using it. Why?

  • @Ciganomorrisonmendez, I put the tag because I’m using Jquery, now if you say it’s easier then show me how you would solve this situation?

  • Quiet. In which field is the road information?

  • Viewbag.idFoto = new Selectlist(dao.fotos, "idFoto", "Photo");

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. No 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 is not going to work. Right would be something like:

$.post("/ConsultaCliente/AtualizaFoto", { caminhofoto: srcRecebe }).done(function (data) {
    $('#caminho').attr("src", data);
})
  • I made the adjustments based on your example, You could help me find out why I’m not able to send the information to Actionresult, I want to return image. thanks

  • I added the full code to my question.

  • @itasouza I modified the answer.

  • I updated the answer. I appreciate your help. http://answall.com/questions/59059/como-chamar-a-mesma-actionresult-mais-de-1-vez-para-actualiz-a-p%C3%A1gina

Browser other questions tagged

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