You must Sign In to post a response.
  • Category: ASP.Net MVC

    Retrieving selected value from database to Cascading

    short i want Retrieving previous selected value from database to Cascading when i click edit button

    i used this jquery
    _________________________________________________________________________________________________
    <script>
    $(function () {
    $("#countryId").on('change', bindStates);
    bindStates();
    });
    function bindStates(){
    $.get("/Country/GetStatesById", { ID: $("#countryId").val() }, function (data) {
    $("#StateId").empty();
    $.each(data, function (index, row) {
    $("#StateId").append(" <option value='" + row.state_id + "'>" + row.name + "</option>")
    });
    })
    }
    </script>
    _________________________________________________________________________________________________
    and this my edit button

    _________________________________________________________________________________________________
    [HttpGet]
    public ActionResult Edite(int id)
    {

    List<Country> countrylist = db.CountryTb.ToList();
    SelectList s2 = new SelectList(countrylist.AsEnumerable(), "id", "name");
    ViewBag.SelectCountry = s2;


    Item I = db.Items.Single(x => x.id == id);

    return View(I);
    }


    [HttpPost]
    [ActionName("Edite")]
    public ActionResult Edite_post(int id)
    {
    Item I = db.Items.Single(x => x.id == id);
    UpdateModel(I, null, null, new string[] { "id" });
    db.SaveChanges();
    return RedirectToAction("Index");
    }
    _________________________________________________________________________________________________

    here my cshtml code
    _________________________________________________________________________________________________
    <!--Country-->
    <div class="form-group">
    @Html.LabelFor(model => model.CountryId, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("countryId", (SelectList)ViewBag.SelectCountry, "select please", new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.CountryId, "", new { @class = "text-danger" })
    </div>
    </div>



    <!--States-->
    <div class="form-group">
    @Html.LabelFor(model => model.StateId, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("StateId", new SelectList(Enumerable.Empty<SelectListItem>(), "state_id", "name"), "-- Select --", new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.StateId, "", new { @class = "text-danger" })
    </div>
    </div>

    <!--City-->
    <div class="form-group">
    @Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("CityId", new SelectList(Enumerable.Empty<SelectListItem>(), "id", "name"), "-- Select --", new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" })
    </div>
    </div>
    _________________________________________________________________________________________________

    so how can i fix that
  • #768313
    Hi,

    I didn't understand your complete code. But I feel we can achieve this implementation easily with the Dropdownlist fire event.

    Suppose consider you have 3 dropdownlist
    1. Country
    2.State
    3.City

    So over here, when the Country dropdown got selected then that fire event we can pass the DDL.text values to the DDL2 and based on DDL1 selection we can display the list in DDL2 and similar for DDL3.

    Use the following method:

    <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack = "true" OnSelectedIndexChanged = "Country_Change">


    and we need to write coding for this selectedIndexchanged functionality,

    protected void Country_Change(object sender, EventArgs e)

    {


    ddlStates.Items.Insert(0, new ListItem("Select State", "0"));

    int countryId = int.Parse(ddlCountry.SelectedItem.Value);

    if (countryId > 0)

    {

    string query = string.Format("select StateId, StateName from table where CountryId = {0}", countryId);

    BindDropDownList(ddlStates, query, "StateName", "StateId", "Select State"); // Binding the values

    ddlStates.Enabled = true; // Setting value to the State DDL

    }

    }


    Hope this helps!!

    Thanks,
    Mani


  • Sign In to post your comments