Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I keep selected values for both dropdown after submit action ?
   In my scenarios My cascaded Dropdown is populating from partial view.
   I new to Core MVC.

What I have tried:

**My view**

    <form asp-controller="Recommendation" asp-action="SubmitData" method="post">
                <select id="States" class="form-control selectpicker" asp-for="StateID" asp- 
                items="@(new SelectList(ViewBag.StateList,"StateID","State"))"
            	placeholder="Select Categories" 										 
            		 @Html.DropDownListFor(m => m.CityID, new SelectList(""), new {@class="select2 
                      form-control", @style = "width: 100%" })    		  
            	<button id="btnSubmit" class="btn btn-secondary btn-sm">Submit</button>   		  

**onChange function on first dropdown to call 2nd one**

    <script type="text/javascript">
                $(document).ready(function () {
                    $("#States").change(function () {
                        var StateID = $(this).val();
                            type: "Get",
                            url: "/Recommendation/GetCityList?iStateID=" + StateID,  
                            contentType: "html",
                            success: function (response) {
                            error: function (jqXHR, textStatus, errorThrown) {

Partial View for Child dropdown 

    <option value="">Select MCA</option>
    		@if (ViewBag.CityOptions != null)
    			foreach(var item in ViewBag.CityOptions)                        
    				<option value="@item.Value">@item.Text</option>


        public ActionResult IndexGet()         
        {   From where I get  values.
		    Entity entity = new Entity();
            StateList = gateway.SelectList();
            StateList.Insert(0, new Model { StateID = 0, State = "Select State" });          
            ViewBag.StateList = StateList;                
            return View();
        public IActionResult SubmitData(RecommendModel recommendModel)       
        {  Submit form method and I used  RedirectToAction for calling view again.
            return RedirectToAction("IndexGet", "Recommendation");
        public ActionResult GetCityList(long iStateID)     
        { For partial call
            Entity entity = new Entity();
            MCAlist = entity.GetCityList(iStateID);
            ViewBag.CityOptions = new SelectList(MCAlist,"MCAID","MCA");      
            return PartialView("_CityOptionPartial");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900