在第一个下拉列表的onchange事件中加载第二个下拉列表

时间:2017-11-24 14:25:38

标签: asp.net-mvc-5

我想在第一个下拉列表的onchange事件中发布一个表单来加载第二个下拉列表。

视图

@using (Html.BeginForm())
{
    <table>

        <tr>
            <td>@Html.LabelFor(s => s.Country)</td>
            <td>@Html.DropDownListFor(s => s.Country, Model.Country, "Select Country", new { @Class = "form-control", @OnChange = "document.location.href = '/Display/StateSelection?Country=' + this.options[this.SelectedIndex].Value;' " })</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(s => s.State)</td>
            <td>@Html.DropDownListFor(s => s.State, Model.State, new { @class = "form-control" })</td>
            @*<td>@Html.DropDownListFor(s => s.State, Model.State, "Select State", new { @Class = "form-control", @onchange = "document.location.href ='/Display/CitySelection?StateID=' + this.options[this.SelectedIndex].Value;" })</td>*@
        </tr>
        @*<tr>
            <td>@Html.LabelFor(s => s.City)</td>
            <td>@Html.ListBoxFor(s => s.City, Model.City, new { @class = "form-control" })</td>
        </tr>*@
    </table>

    @*<input type="submit" value="DisplayData" />*@
    @*<input type="submit" value="DisplayData" onclick="location.href = '@Url.Action("DisplayView","Display")'"/>*@
}

控制器

    public ActionResult DisplayView()
    {
        DisplayModel obj = new DisplayModel();
        string CountryID = "0";
        //string StateID = "0";
        List<SelectListItem> CountryList = obj.CountryDetails();
        List<SelectListItem> StateList = obj.StateDetails(CountryID);
        //List<SelectListItem> CityList = obj.CityDetails(StateID);
        //var CountryList = obj.CountryDetails(dm);

        obj.Country = CountryList;// new SelectList(CountryList, "Value", "Text");
        obj.State = StateList;
        //obj.City = CityList;
        TempData["CountryList"] = CountryList;

        return View("DisplayView",obj);
    }


    public ActionResult StateSelection(String Country)
    {
        DisplayModel obj = new DisplayModel();
        obj.Country = (List < SelectListItem >)TempData["CountryList"];
        obj.State = obj.StateDetails(Country);
        TempData.Keep("CountryList");
        return View("DisplayView",obj);
    }


From view, I'm passing the selected value to the controller, from controller calling the logic from model to load the list based on the country selected.

当我提交表单时,它按预期工作。任何人都可以告诉我如何根据在OnChange事件的第一个下拉列表中选择的值加载第二个下拉列表,而无需使用&#34;提交按钮&#34;提交表单。

0 个答案:

没有答案
相关问题