Populate DropDownList using AJAX MVC 4

前端 未结 3 1590
星月不相逢
星月不相逢 2020-12-09 04:34

I have a view in place that contains 2 @DropDownListFor\'s Helpers:

    @using (Html.BeginForm(\"CreateOneWayTrip\", \"Trips\"))
    {
        @Html.Validati         


        
3条回答
  •  有刺的猬
    2020-12-09 05:27

    Can you try the following?

    This is a post that i answared some days ago. Font:Dynamic DropDownLists In MVC 4 Form

    You should create a ajax call in the change event of the province ddl. This call will request to an action and return the cities of selected province.

    $("province_dll").change(function(){
        $.ajax({
             url: 'getCitiesController/getCitiesAction',
             type: 'post',
             data: {
                   provinceId: provinceIdVar
             }
        }).done(function(response){
             $("cities_dll").html(response);
        }); 
    });
    

    In the action:

    [HttpPost]
    public ActionResult getCicitesAction(int provinceId)
    {
         var cities = db.cities.Where(a => a.provinceId == provinceId).Select(a => "'";
    
         return Content(String.Join("", cities));
    }
    

提交回复
热议问题