I want to populate text box with dropdown selectio

2019-09-07 06:18发布

问题:

I have a table compte with

  • id
  • login
  • mail
  • mobile

I want to display the login with a dropdown list and when the user selects login the mobile and the email should be displayed automatically in 2 text boxes based on his selection.

Can someone help or give me an example?

回答1:

I am sure you are not expecting us to write the code from scratch , but looking for some steps how to achieve this

1) Get All Login Column values from your Data Access layer (table/Cache) and Show that in a DropDown.

2) Listen to the change event of the Dropdown , in javascript. When the change event occurs, get the selected Item's value and Make an Ajax call to another Action method and pass the selected value of the Dropdown as a parameter of this method .The Action method should get the corresponding record from your table for this purticular login and return the result in JSON format to the client.

3) in the call back of your ajax method, Read the JSON and Set the value of the Text boxes.

$(function(){
    $("#LoginDropDown").change(function(){
      var val=$(this).val();

       $.getJSON("@Url.Action("GetDetails","Home")/"+val,function(data){
            if(data.Status=="Success")
            {
              $("#txtMail").val(data.Mail);
              $("#txtMobile").val(data.Mobile);
            }    
        });    
    });      
});

Assuming your Action method Returns a JSON in this format.

{
    "Status": "Success",
    "Mail": "you@outlook.com",
    "Mobile": "242342344"
}