可以刷新视图中的 ViewBag 值吗?

It is possible to refresh ViewBag value in the view?

我正在尝试做一个动态下拉列表: 我从数据库中获取下拉列表的选项并将它们放入对象列表中。根据复选框值,我从列表中删除对象并将此列表设置为 ViewBag 值。

public ActionResult ThematicManagement(string Id, string IsAdult, string flagAdult)
             {
                .....
                ViewBag.DDL = null;      

                var response = VodCatalogBUS.GetParentThematics(); 
                List<oboThematic> list = new List<oboThematic>();
                list = response.Data;
                if (IsAdult == null || IsAdult == "false")            
                    list.RemoveAll(x => x.IsAdult == true && x.Id != 1007);           
                else 
                    list.RemoveAll(x => x.IsAdult == false && x.Id != 1007);                

                ViewBag.DDL = new SelectList(list, "Id", "Name");
                ....   

然后在我看来,我会像这样填写下拉列表:

 @Html.DropDownList("ParentThematic", (SelectList)ViewBag.DDL, new { @class="dropdown" })    

<label><input type="checkbox" value="" id="ChkIsAdult" name="ChkIsAdult">Adulte</label>

这里没有问题,我在控制器中的RemoveAll之后获得了带有4个选项的下拉列表。然后如果我点击复选框,我必须获得 3 个其他选项。

所以我在控制器中使用 ajax 调用 return 来更新 Viewbag 的值:

 $('#ChkIsAdult').change(function () {                 
                var IsAdult = $('#ChkIsAdult').is(':checked');
                var url = dev + "/Legacy/ThematicManagement";
                $.ajax({
                    url: url,
                    cache: false,
                    type: 'POST',
                    data: {                        
                        IsAdult: IsAdult,
                        flagAdult : 'true',
                    },
                    success: function () {
                        alert('test');
                    }
                });               
            })

它在控制器中工作 return,但我认为视图没有刷新,所以我在单击复选框后检索了下拉列表的旧值(4 个选项)。

我也尝试用 ViewData 和 TempData 来替换 ViewBag,但我一直都是一样的 proprem!

您认为这是好的解决方案吗?能行吗?

这是回复:

控制器

 var response = VodCatalogBUS.GetParentThematics(); 
        List<oboThematic> list = new List<oboThematic>();

        list = response.Data;

            list.RemoveAll(x => x.IsAdult == true && x.Id != 1007);

            var responseAdult = VodCatalogBUS.GetParentThematics(); 
            List<oboThematic> listAdult = new List<oboThematic>();
            listAdult = responseAdult.Data;
            listAdult.RemoveAll(y => y.IsAdult == false && y.Id != 1007);                

        ViewBag.DDL = new SelectList(list, "Id", "Name");
        ViewBag.DDLAdult = new SelectList(listAdult, "Id", "Name");

查看:

  @Html.DropDownList("ParentThematic", (SelectList)ViewBag.DDL, new { @class="dropdown" })
@Html.DropDownList("ParentThematicAdult", (SelectList)ViewBag.DDLAdult, new { @class="dropdown" , @style="display:none"})

JS :

$('#ChkIsAdult').change(function () {                 
                if ($('#ChkIsAdult').is(':checked')) {
                    $('#ParentThematic').hide();
                    $('#ParentThematicAdult').show();
                    var value = $('#ParentThematicAdult').val();
                    var IsAdult = $('#ChkIsAdult').is(':checked');
                    var url = dev + "/Legacy/ThematicManagement";
                    $.ajax({
                        url: url,
                        cache: false,
                        type: 'POST',
                        data: {
                            Id: value,
                            IsAdult: IsAdult
                        },
                        success: function (data) {
                            $('#result').empty().append($(data).find('table'))
                        }
                    });

                }
                else
                {
                    $('#ParentThematic').show();
                    $('#ParentThematicAdult').hide();
                    var value = $('#ParentThematic').val();
                    var IsAdult = $('#ChkIsAdult').is(':checked');
                    var url = dev + "/Legacy/ThematicManagement";
                    $.ajax({
                        url: url,
                        cache: false,
                        type: 'POST',
                        data: {
                            Id: value,
                            IsAdult: IsAdult
                        },
                        success: function (data) {
                            $('#result').empty().append($(data).find('table'))
                        }
                    });
                }                            
            })