You must Sign In to post a response.
  • Category: LINQ

    Select item from city not fill items in district drop down although database have value

    I have 4 table relation in sql server database is good

    Country table

    Id primarykey increment identity

    Countryname

    City table

    Id primary key increment identity

    Cityname

    CountryId forignkey

    Ditrict table

    Id primarykey increment identity

    Districtname

    CityId forignkey

    Employee table

    Id primary key increment identity

    fname

    sname

    salary

    Bonus

    districtid forignkey

    Relation not have any proplem

    when select items from country it fill city drop down list without any problem

    but when i select item from city it not fill items in district why

    something wrong in my code i do as following
    my controller employee as following

    Employee Controller
    public class EmployeeController : Controller
    {

    // GET: Employee
    mytaskdbEntities db = new mytaskdbEntities();
    public ActionResult Index()
    {
    return View(db.Employees.ToList());
    }
    public ActionResult Create()
    {
    ViewBag.CountryList = new SelectList(db.Countries.ToList(), "Id","Countryname");

    return View();
    }
    public JsonResult getcitybyid(int id)
    {
    db.Configuration.ProxyCreationEnabled = false;
    return Json(db.Cities.Where(a =>a.CountryId == id), JsonRequestBehavior.AllowGet);
    }
    public JsonResult getdistrictbyid(int id)
    {
    db.Configuration.ProxyCreationEnabled = false;
    return Json(db.Districts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);
    }
    }
    }



    my Employee view is

    @model LinqProject.Models.Employee
    @{
    Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Create</title>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script>
    $(function () {
    $("#CountryList").change(function () {
    $("#citylist").empty();
    // alert("error");
    var x = $(this).val();
    $.ajax({
    url: "/Employee/getcitybyid",
    data: { id: x },
    success:function(res)
    {
    $.each(res, function (i, e) {
    $("#citylist").append("<option value='"+e.id+"'>"+e.Cityname+"<option>")

    });
    }
    });


    });
    $("#citylist").change(function () {
    $("#districtlist").empty();
    var y = $(this).val();
    $.ajax({
    url: "/Employee/getdistrictbyid",
    data: { id: y },
    success: function (res) {
    $.each(res, function (i, e) {
    $("#districtlist").append("<option value='" + e.id + "'>" + e.Districtname + "<option>")

    });
    }
    });


    });
    });
    </script>
    </head>
    <body>
    <div>
    @using (Html.BeginForm())
    {
    <div>
    FirstName:@Html.TextBoxFor(a=>a.fname)

    </div>
    <div>
    LastName:@Html.TextBoxFor(a => a.sname)

    </div>
    <div>
    Salary:@Html.TextBoxFor(a => a.Salary)

    </div>
    <div>
    Bonus:@Html.TextBoxFor(a => a.Bonus)

    </div>
    <div>
    Bonus:@Html.TextBoxFor(a => a.Active)

    </div>
    <div>
    CountryName:@Html.DropDownList("CountryList")

    </div>
    <div>
    CityName:<select id="citylist" name="CityId"></select>
    </div>
    <div>
    District:<select id="districtlist" name="districtId"></select>
    </div>
    <input type="submit" />
    }
    </div>
    </body>
    </html>
  • #767573
    Hi,

    Make sure you have to call the selected columns only, if your selected columns increases then automatically the no of rows also increased, so use DISTINCT keyword to remove your duplicates.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767574
    There are several ways you can populate data for a dependent Dropdown list. Let me explain what I mean by dependent Dropdown list. For example, in your application, user need to select a Country. Based on user selection, you need to show only the State/Province of the selected country. Again, when user selects a State/Province, you need to show only the Cities of the selected State/Province
    you can use either XML or database to select Country/State/City combo. I got some links below that may helpful to you
    see below snippet
    http://www.codeproject.com/Tips/850816/Dynamically-Populate-Dependent-Dropdown-List-in-MV
    http://aspsolutionkirit.blogspot.in/2013/12/bind-countrystatecity-in-dropdownlist.html

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767729
    Hai Ahmedsa,
    You need to check 2 things:
    1. What is the value passing to this function:

    public JsonResult getdistrictbyid(int id)
    {
    db.Configuration.ProxyCreationEnabled = false;
    return Json(db.Districts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);
    }

    2. What is value of y variable which is passing in the below code

    var y = $(this).val();
    $.ajax({
    url: "/Employee/getdistrictbyid",
    data: { id: y },

    Hope it will be helpful to you to resolve the issue.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767730
    Following are some the tips to identify your issue.
    Is the onChange Event firing for City selection?
    If the event is not firing fix that by adding the onchange envent on the drop box.
    If it is firing the event are you getting the data from the DB?
    If above two are fine. Just put debugger/alert box and make sure are you adding the value in the dropdown in the ajax Sucess.

    By Nathan
    Direction is important than speed

  • #767731
    Hi

    you can follow this code

    add property layer this


    public partial class State
    {
    public int StateId { get; set; }
    public string StateName { get; set; }
    public string Abbr { get; set; }
    }
    public partial class City
    {
    public int CityId { get; set; }
    public string CityName { get; set; }
    public int StateId { get; set; }
    }



    view



    <div class="form-group">
    @Html.LabelFor(m => m.State, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownListFor(m => m.State,
    new SelectList(ViewBag.StateList, "StateId", "StateName"),
    "Select state",
    new { @class = "form-control", @onchange="FillCity()" })
    @Html.ValidationMessageFor(m => m.State, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(m => m.City, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownListFor(m => m.City,
    new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"),
    "Select city",
    new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.City, "", new { @class = "text-danger" })
    </div>
    </div>




    Calling Method



    @section Scripts {
    <script>
    function FillCity() {
    var stateId = $('#State').val();
    $.ajax({
    url: '/Employees/FillCity',
    type: "GET",
    dataType: "JSON",
    data: { State: stateId},
    success: function (cities) {
    $("#City").html(""); // clear before appending new list
    $.each(cities, function (i, city) {
    $("#City").append(
    $('<option></option>').val(city.CityId).html(city.CityName));
    });
    }
    });
    }
    </script>
    }



    in Controller



    public ActionResult FillCity(int state)
    {
    var cities = db.Cities.Where(c => c.StateId == state);
    return Json(cities, JsonRequestBehavior.AllowGet);
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments