是否可以刷新视图中的ViewBag值? [英] It is possible to refresh ViewBag value in the view?
问题描述
我正在尝试创建动态下拉列表: 我从数据库中获得了下拉列表的选项,并将它们放在对象列表中.按照复选框的值,我从列表中删除对象,并将此列表设置为ViewBag值.
I'm trying to do a dynamic dropdownlist : I get the options of the dropdownlist from a database and put them in an objects list. In accordance with a checkbox value i remove objects from the list and set this list as a ViewBag value.
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");
....
然后在我看来,我会像这样填写下拉列表:
Then in my view i fill the dropdownlist like that :
@Html.DropDownList("ParentThematic", (SelectList)ViewBag.DDL, new { @class="dropdown" })
<label><input type="checkbox" value="" id="ChkIsAdult" name="ChkIsAdult">Adulte</label>
这里没有问题,我在控制器中的RemoveAll之后获得带有4个选项的下拉列表.然后,如果我单击复选框,则必须获得其他3个选项.
There is no problems here, i obtain the dropdown list with 4 options after the RemoveAll in the controller. Then if i click the checkbox, i must obtain 3 other options.
所以我使用ajax调用返回到控制器,以更新Viewbag的值:
So I use an ajax call to return into the controller in the aim to update Viewbag's value :
$('#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');
}
});
})
它可以正常工作,但我返回到控制器,但是我认为该视图不会刷新,因此在单击复选框后,我会检索下拉列表的旧值(4个选项).
It works i return into the controller, but i think that the view isn't refresh so i retreive the old values (the 4 options) of the dropdownlist after clicking the checkbox.
我也尝试用ViewData和TempData替换ViewBag,但是我总是有相同的建议!
I also try with ViewData and TempData to replace ViewBag but i've always the same proprem !
据您所述,这是一个好的解决方案?能行吗?
According to you, it is the good solution ? Can it works ?
推荐答案
以下是响应:
控制器
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'))
}
});
}
})
这篇关于是否可以刷新视图中的ViewBag值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!