通过mvc中的控制器启用html下拉菜单

发布于 2024-09-28 20:38:13 字数 3234 浏览 1 评论 0原文

我有一个场景,我必须根据选择执行一些操作 下拉菜单。

作为基本参考,您可以使用国家、州、城市的示例。

我当时能够填充国家/地区列表,我已将其他两个下拉菜单设置为 禁用。

在国家/地区填充后,我想选择州。它正在填充。

两个问题

1)当国家 ddl 回到原来的状态时如何保持它的状态。 2)如何通过我的控制器启用下拉菜单。

我的视图代码

国家

     <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>

我的控制器代码

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2024-10-05 20:38:13

我的选择是根本不发回表格。我会在控制器中编写一个操作,该操作采用 CountryID 并返回包含州列表的 JsonResult。 onchange 事件可以调用 jQuery 函数,该函数使用 AJAX 来调用此操作、加载新列表并启用第二个下拉列表。

但是,如果您坚持使用回发,则它不起作用的原因如下:

  1. 国家/地区列表不会保留其选定的值,因为每次都会从头开始重新加载视图,并且您将其设置为“未选择”。 SelectList 构造函数有一个重载,它将“SelectedItem”对象作为第四个参数。当您初始化 SelectList 时,您应该将适当的值传递给此参数,而不是在视图中强制它。

  2. 您需要在视图中编写一个“if”子句,以根据某些条件选择是否启用该列表。您可以绑定到具有布尔属性(如“EnableStates”)的 ViewModel,或者您可以使用 StateList 中的值计数之类的内容 - 例如,如果计数大于零,则启用它。

当您从 Web Forms 迁移到 MVC 时,需要适应的一件棘手的事情是您不再拥有 ViewState - 您的应用程序是无状态的。没有什么可以“记住”在下拉列表中为您选择的值,您必须在每次加载页面时进行设置。

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.

提笔落墨 2024-10-05 20:38:13

我建议使用 JSON & jQuery - 就像这个发布的答案

I recommend using JSON & jQuery - like this posted answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文