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 dbreturn Json(person);
}

Then in your view, you have

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

    ID:@Html.DropDownList("Id", newSelectList(ViewBag.List, "Value", "Text"), null,  new { @id= "Id"})
    <br />
    FirstName: <inputtype="text"name="FName"id="FName" /><br />MiddleName: <inputtype="text"name="MName"id="MName" /><br />LastName: <inputtype="text"name="LName"id="LName" /><br />DateofBirth:<inputtype="date"id="start"name="DOB"value="2018-07-22"min="1900-01-01"max="2000-12-31" /><br />NIC:<inputtype="text"name="NIC" /><br />Address:<inputtype="text"name="Adddress" /><br /><inputtype="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"