0

I want my users to select an item in a dropdownbox and once selected, I want the other fields associated to this value selected to populate the text boxes. As you change values on the dropdown, the records in the text boxes changes.

For example if you select customerId of 1001 the FirstName, LastName and EmailAddress assosciated to 1001 should populate the text boxes for the 3 fields. CustomerId="1001", FirstName="John", LastName="Travolta", EmailAddress="John.Travolta@yahoo.com"

Here is the controller code

         public class HomeController : Controller
            {
                public ActionResult Index()
                {
                    var list = new SelectList(new[] 
                    {
                        new { ID = "1", Name = "Select Id" },
                        new { ID = "2", Name = "1001" },
                        new { ID = "3", Name = "1002" },
                        new { ID = "4", Name = "1003" },
                    },
                    "ID", "Name", 1);

                    ViewData["list"] = list;

                    return View();
                }

                public List<Customer> GetCustomerById(String CustomerId)
                {
                    var objCustomer = new List<Customer>();

                    objCustomer.Add(new Customer {CustomerId="1001", FirstName="John", LastName="Travolta", EmailAddress="John.Travolta@yahoo.com"});
                    objCustomer.Add(new Customer {CustomerId = "1002", FirstName = "Bayo", LastName = "Adenuga", EmailAddress = "Bayo.Adenuga@yahoo.com" });
                    objCustomer.Add(new Customer { CustomerId = "1003", FirstName = "Feyi", LastName = "Olawoye", EmailAddress = "feyi.olawoye@yahoo.com" });


                    var objResult = from c in objCustomer
                                    where c.CustomerId == CustomerId
                                    select c;

                    return objResult.ToList();
                }

                [HttpPost]
                public ActionResult Action(string customerId)
                {     
                    var results = GetCustomerById(customerId);           
                    return Json(results);
                }

            }

Here is my code for the view

            @model DropdownSelectedFillTextBox.Models.Customer

        @{
            ViewBag.Title = "Home Page";
        }

        <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js">
        </script>

        @using (@Html.BeginForm("Action","Home",FormMethod.Post))
        {

             <h2>Dotuns Test</h2>

            <div>
                @Html.DropDownList("drpList",ViewData["list"] as SelectList)
            </div>

            <div>First Name</div>
            <div>
                  @Html.TextBox("FirstName", null, new { @class = "form-control" })            
            </div>

            <div>Last Name</div>
            <div>
                  @Html.TextBox("LastName", null, new { @class = "form-control" })         
            </div>

            <div>Email Address</div>
            <div>
                 @Html.TextBox("EmailAddress", null, new { @class = "form-control" })
            </div>

        }

           <script type="text/javascript">

               function Action(customerId) {
            $.ajax({
                url: '@Url.Action("Action", "Home")',
                type: "POST",
                data: { "customerId": customerId },
                "success": function (data) {
                    if (data != null) {
                        var vdata = data;
                        $("#FirstName").val(vdata[0].FirstName);
                        $("#LastName").val(vdata[0].LastName);
                        $("#EmailAddress").val(vdata[0].EmailAddress);
                    }
                }
            });
        }
        </script>

I changed the jquery to the code below. I put an alert box there and that is getting hit but nothing is happening. When I put a break point on the method in the controller. It does not hit the break point.

    <script type="text/javascript">
           $('#drpList').change(function () {
               alert("Hello Test");
               $.ajax({
                   url: '@Url.Action("Action", "Home")',
                   type: "POST",
                   data: { "customerId": customerId },
                   "success": function (data) {
                       if (data != null) {
                           var vdata = data;
                           $("#FirstName").val(vdata[0].FirstName);
                           $("#LastName").val(vdata[0].LastName);
                           $("#EmailAddress").val(vdata[0].EmailAddress);
                       }
                   }
               });

           });
    </script>
P6345uk
  • 703
  • 10
  • 26
Baba
  • 2,059
  • 8
  • 48
  • 81
  • Where do you call `function Action(customerId)`? It would be better to use `$('#drpList').change(function() { // your ajax call });` –  Jun 29 '15 at 04:22
  • You should also be using `.FirstOrDefault()` on your query, and then just `$("#FirstName").val(data.FirstName);` - no point sending back an array when you only want the first object. –  Jun 29 '15 at 04:41
  • Thanks I changed the code to what you suggested and nothing happens. – Baba Jun 29 '15 at 12:42
  • 1
    Success with out the quotes? – P6345uk Jun 29 '15 at 12:43
  • also you can add error onto the ajax call to get you details of any errors - would you like me to add details? http://stackoverflow.com/questions/2833951/how-to-catch-ajax-query-post-error – P6345uk Jun 29 '15 at 12:46
  • What do your mean _"nothing happens"_? Do you get the alert ("Hello Test")? Nowhere in your code have you assigned a value to `customerId` - its should be `data: { "customerId": $(this).val() },` and as @P6345uk has pointed out, you need to remove the quotes around `success:`. Next use you browser tools to debug your code. –  Jun 29 '15 at 12:48
  • Nothing happens meaning the text boxes are not been populated. I will try all what you suggested now and get back to you. – Baba Jun 29 '15 at 12:57
  • yes P6345uk feel free to add details. Thanks – Baba Jun 29 '15 at 12:58
  • I am getting there. success: function (data) ... data is coming as object. values are not been displayed in the text boxes. How do I handle this? Do I need some kind of conversion? – Baba Jun 29 '15 at 13:44
  • Did you manage to get this solved? – P6345uk Jul 03 '15 at 08:50
  • Yes I did. I had to write a loop and loop through the data to retrieve the individual fields though. – Baba Jul 03 '15 at 23:51
  • Data returned as object – Baba Jul 03 '15 at 23:52

0 Answers0