views:

54

answers:

1

i have an scenario where i have to perform some action according to selection of the dropdown .

for basic refrence you can use the example of country,state,city.

i am able to populate the country list at that time i have set the other two drop downs to disable.

after the countries are populated i want to select the state.it is getting populated .

two problems

1) how to retain the state of country ddl as it is coming back to its orisnal state. 2) how to enable the drop down through my controller.

myview code

Country

     <p>
        <label>
            State</label>
        <%=Html.DropDownList("ddlState", ViewData["ddlState"] as SelectList, "--not selected--",new { onchange = "document.forms[0].submit()", disabled = "disabled" })%>
    </p>

     <p>
        <label>
            City</label>
        <%=Html.DropDownList("ddlCities", ViewData["ddlCities"] as SelectList, "--not selected--", new { onchange = "document.forms[0].submit()", disabled = "disabled" })%>
    </p>

my controller code

    public ActionResult InsertData()
    {

        var customers = from c in objDetailEntity.Country
                        select new
                        {
                            c.Cid,
                            c.Cname
                        };

        SelectList countriesList = new SelectList(customers.Take(100), "Cid", "Cname");
        ViewData["ddlCountries"] = countriesList;

        SelectList EmptyState = new SelectList(customers);
        ViewData["ddlState"] = EmptyState;
        ViewData["ddlCities"] = EmptyState;
        Session["ddlSesCountry"] = countriesList;
        return View();

    }




    //
    // POST: /RegisTest/Create

    [HttpPost]
    public ActionResult InsertData(FormCollection collection)
    {
        try
        {
            CountryId = Convert.ToInt16(Request.Form["ddlCountries"]);
            stateid = Convert.ToInt16(Request.Form["ddlState"]);
            if (CountryId > 0 && stateid <= 0)
            {
                var stateslist = from c in objDetailEntity.State
                                 where c.Country.Cid == CountryId
                                 select new
                                 {
                                     c.Sid,
                                     c.Sname
                                 };

                SelectList stateList = new SelectList(stateslist.Take(100), "Sid", "Sname");
                ViewData["ddlState"] = stateList;
                Session["StateList"] = stateList;
                ViewData["ddlCities"] = stateList;
            }

            if (stateid > 0)
            {
                var citieslist = from c in objDetailEntity.City
                                 where c.State.Sid == stateid
                                 select new
                                 {
                                     c.Ctid,
                                     c.Cityname
                                 };

                SelectList cityList = new SelectList(citieslist.Take(100), "Ctid", "Cityname");


                ViewData["ddlCities"] = cityList;
                ViewData["ddlState"] = Session["StateList"];
            }

            ViewData["ddlCountries"] = Session["ddlSesCountry"];



            return View();
        }
        catch
        {
            return View();
        }
    }
A: 

My choice would be not to post back the form at all. I would write an action in the controller that takes a CountryID and returns a JsonResult holding a list of states. The onchange event could call a jQuery function that uses AJAX to call this action, loads the new list, and enables the second drop-down list.

However, if you stick with the postback, here's why it's not working:

  1. The Country list isn't retaining its selected value because the view is being reloaded from scratch each time and you're setting it to "not selected." The SelectList constructor has an overload that takes a "SelectedItem" object as the fourth parameter. When you initialize your SelectList, you should pass the appropriate value to this parameter, and not force it in the View.

  2. You need to write an "if" clause in your View to choose whether or not to enable the list based on some criteria. You could bind to a ViewModel that has a Boolean property like "EnableStates," or you could use something like the count of values in the StateList - if the count is greater than zero, enable it, for example.

A tricky thing to get used to when you move from Web Forms to MVC is that you don't have ViewState anymore - your application is stateless. There's nothing that "remembers" what value is selected in a drop-down for you, you have to set it each time you load the page.

Jeremy Gruenwald
can you provide some code for both 1 and 2 condition
Ankur