Click here to Skip to main content
15,918,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using ajax to populate my Dropdownlist but i have no idea how to solve this issue.

C#
public partial class ApplicationFormFarmersSolarPower
    {
        
        public int Id { get; set; }
        public string UniqueId { get; set; }
 
        //[Required(ErrorMessage = "Please select District.")]
        public  string District { get; set; }
 
        //[Required(ErrorMessage = "Please select Tehsil.")]
        public  string Tehsil { get; set; }
 
        //[Required(ErrorMessage = "Please select SubTehsil.")]
        //public string SubTehsil { get; set; }
        //[Required(ErrorMessage = "Please select District.")]
        public  string DistrictName { get; set; }
 
        //[Required(ErrorMessage = "Please select Tehsil.")]
        public  string TehsilName { get; set; }
 
        //[Required(ErrorMessage = "Please select SubTehsil.")]
        public string SubTehsilName { get; set; }
 
        public string Village { get; set; }
}


C#
[HttpGet]
        public JsonResult GetTehsil(string districtID = "")
        {
            List<Tehsil> allTehsil = new List<Tehsil>();
            int ID = 0;
           
            if (int.TryParse(districtID, out ID))
            {
                using (FarmerBDContext db = new FarmerBDContext())
                {
                    allTehsil = db.Tehsils.Where(a => a.DistrictId.Equals(ID)).OrderBy(a => a.TehsilName).ToList();
                 
                }
            }
 
            if (Request.IsAjaxRequest())
            {
                return new JsonResult
                {
                    Data = allTehsil,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                return new JsonResult
                {
                    Data = "Not Valid request",
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
        }


JavaScript
<script type="text/javascript">


    $(document).ready(function () {
        $("#DistrictName").change(function () {

            var districtID = parseInt($("#DistrictName").val());
            
            if (!isNaN(districtID)) {
                var ddState = $("#TeshilName");
                ddState.empty();
                ddState.append($("<option></option").val("").html("Select State"));
                
                $.ajax({
                    url: "@Url.Action("GetTehsil", "Home")",
                    type: "GET",
                    data: { "districtID": districtID },
                    
                    dataType: "json",

                    success: function (Data) {
                        $.each(Data, function (i, valueOf) {
                            ddState.append(
                                    $("<option></option>").val(valueOf.TehsilId).html(valueOf.TehsilName)
                                );
                        });
                        console.log(JSON.parse(data));
                    },
                    error: function () {
                        alert("Error This is It!");
                    }
                });
            }
        });
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900