For this, you can make a method that returns all users in Json
in his controller
, would look like this:
Fetch 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 his View
, you can use the query Ajax
, as @Kaduamaral suggested, so:
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>
}
Face lack more clarity in the question. How do you want to select this field and where to answer? A select think solves the selection of the field, type: select campo1 from table. That (s) result(s) you take and put in a textbox, label and etc. Now, by the question I could not understand well.
– pnet
Do you want to select this data from where exactly? Are you using any database? Which one? If it is a database, which tables do you want to search the data for? If it is not a database, what data structure is it using? Many questions need to be answered so that we can answer your...
– KaduAmaral
Let me try to explain better, I am making a web system, has a field where I select the user, when selecting the user I wanted to bring all the information related to this user, as for example Name, Address, Cpf. I could tell?
– Italo
I wanted to pull this information from the database. This user would have been previously registered.
– Italo
I’m doing a dropdown that pulls all registered users, when selecting one, pull all this information from the database, filling these fields.
– Italo
Right, get the information in the server-side database can you? You just want to know how to search and show?
– KaduAmaral
Get the server side information I can, I want to play this information to the user side, to be displayed on the screen.
– Italo