简体   繁体   English

在 ASP.NET Core MVC 中提交后,我的级联下拉列表丢失了选定的值?

[英]My cascaded dropdowns losing selected values after submit in ASP.NET Core MVC?

How can I keep selected values for both dropdown after submit action?提交操作后,如何保留两个下拉列表的选定值?

In my scenarios, my cascaded dropdown is populating from partial view.在我的场景中,我的级联下拉列表是从部分视图中填充的。 I'm new to ASP.NET Core MVC.我是 ASP.NET 核心 MVC 的新手。 Let me know if you want more clarifications.如果您需要更多说明,请告诉我。

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"                                          
                 onchange="console.log($(this).children(':selected').length)">
    </select>              
    @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>           
 </form>

onChange function on first dropdown to call 2nd one: onChange function 在第一个下拉列表中调用第二个:

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

Partial View for Child dropdown子下拉菜单的部分视图

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

Controller: Controller:

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

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult SubmitData(RecommendModel recommendModel)       
{  // Submit form method and I used  RedirectToAction for calling view again.
        {            
        }            

        return RedirectToAction("IndexGet", "Recommendation");
}

[HttpGet]
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");
} 

You can send same model again in post action.您可以在后期操作中再次发送相同的 model。

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult IndexGet(RecommendModel recommendModel)       
{                        
     return View(model);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM