Search for database information by selecting a field [closed]

0

How to select a field and pull the information about it?

For example, I wanted to select a user and when I select it appears in the fields type Name, Address, cpf and etc all filled in, searching for this information from the database.

I'm using ASP .NET, C #, HTML, javascript.

If anyone can give a boost there, thank you.

    
asked by anonymous 09.06.2015 / 13:36

3 answers

0

For this, you can do a method that returns all users in Json to their controller , it would look like this:

GetPorId method in controller:

    public JsonResult ObterPorId(int id)
        {  
 //ViewBag para listar todos os usuários
 ViewBag.Usuarios = Context.Usuarios.ToList()
                .Select(e => new SelectListItem
                {
                    Value = e.UsuarioId.ToString(),
                    Text = e.Nome,                    
                });
            var usuarios= context.usuarios.where(u => u.id == id).FirstOrDefault();

            return Json(usuarios, JsonRequestBehavior.AllowGet);
        }

And in your View , you can use the query Ajax , as @KaduAmaral suggested, thus:

View:

<div class="col-md-2">
    Usuarios:
    @Html.DropDownList("ddlUsuarios", new SelectList(ViewBag.Usuarios, "value", "text"), new { @class = "form-control" })
</div>


id: <input id="id" name="id" /><br/>
Nome: <input id="nome" name="nome" /><br />
Cpf: <input id="cpf" name="cpf" />

@section Scripts {
    <script type="text/javascript">
        $('#ddlUsuarios').change(function () {
           // Valor atual selecionado
           var user = $(this).val();
           $.ajax({
               url: @Url.Content("~/") +'Usuarios/ObterPorId',
               type: 'GET',
               dataType: 'json',
               // No servidor com C# vai receber como Request.QueryString["usuario"]
               data: { id: user },
               success: function (json) {
                   // "nome" deve ser o id do campo input ex:
                   // <input type="text" name="nome-do-usuario" id="nome">
                   // Segue mesma regra para demais campos
                   //Lembrando que aqui e sensitive case, ou seja, diferenciando letras maiúsculas de minúsculas. 
                   $('#id').val(json.id);
                   $('#nome').val(json.nome);
                   $('#cpf').val(json.cpf);

               },
               error: function (e) {
                   alert("Deu algo errado, examine o console para mais detalhes");
                   console.log(e);
               }
           });
       });

    </script>
}
    
09.06.2015 / 15:10
1

Use an ajax request to send the request with the data of the selected user:

// <select id="usuario">...</select>
// Instrução será chamada assim que o campo usuario for alterado
$(document).on('change', '#usuario', function(event){
    // Valor atual selecionado
    var user = $(this).val();
    $.ajax({
        url: 'endereco/da/pagina.aspx',
        type: 'GET',
        dataType: 'json',
        // No servidor com C# vai receber como Request.QueryString["usuario"]
        data: {usuario: user}, 
        success: function(json){
            // "nome" deve ser o id do campo input ex:
            // <input type="text" name="nome-do-usuario" id="nome">
            // Segue mesma regra para demais campos
            $('#nome').val(json.nome);
            $('#cpf').val(json.cpf);
            $('#endereco').val(json.endereco);
        },
        error: function (e){
            alert("Deu algo errado, examine o console para mais detalhes");
            console.log(e);
        }
    });
});

Using jQuery you get a great result with just a few lines. However you can get the same result with pure Javascript, just talk a little about AJAX . And to enter the values in the fields you can use an instruction similar to:

var json = JSON.parse(xmlhttp.responseText);
document.getElementById("nome").value = json.nome;

I do not program in ASP.NET, so I will not be posting server code, but to learn more about how to code a json see the MSDN .

    
09.06.2015 / 14:32
0

Dude, it's hard to answer your question because there's too little information on how your application is doing and how you're accessing the data, but I think what you want is something like the following link example.

link

    
09.06.2015 / 14:03