Skip to content Skip to sidebar Skip to footer

How To Populate Details Fields On Combo Box Selected Item Change In ASP.net MVC

I have following controller class methods for get and post actions of updating a person record in my MVC application using a WCF service. There I'm using a combobox to select the I

Solution 1:

Lets say you have this in your controller

public JsonResult GetPersonDetails(int Id){
    var person = db.Person.Where(m => m.Id == Id); //this should be accessed from the db
    return Json(person);
}

Then in your view, you have

<form method="post" action="@Url.Action("updatePerson")">

    ID:@Html.DropDownList("Id", new SelectList(ViewBag.List, "Value", "Text"), null,  new { @id= "Id"})
    <br />
    First Name: <input type="text" name="FName" id="FName" />
    <br />
    Middle Name: <input type="text" name="MName" id="MName" />
    <br />
    Last Name: <input type="text" name="LName" id="LName" />
    <br />
    Date of Birth:<input type="date" id="start" name="DOB" value="2018-07-22" min="1900-01-01" max="2000-12-31" />
    <br />
    NIC:<input type="text" name="NIC" />
    <br />
    Address:<input type="text" name="Adddress" />
    <br />
    <input type="submit" value="Insert" />

</form>

make sure to give all inputs Id variable

Then you will have a Javascript function to call the controller

$("#Id").change(function(){
   var  value = $("#Id").val();
   //$.get(URL,data,function(data,status,xhr),dataType)
   $.get(
        "@Url.Action("GetPersonDetails")", 
        {id:value},
        function (response) {    
            $("#FName").val(response.FName);
            //assign all other variables here
        },
        "json"
   );
});

Post a Comment for "How To Populate Details Fields On Combo Box Selected Item Change In ASP.net MVC"