根据viewbag设置默认下拉默认值
我有一个下拉列表,当其选定值更改时,它会重新加载页面。这可以按预期工作并呈现。唯一的问题是下拉列表返回到默认值。如何更改默认值以匹配 ViewBag.Value?
@Html.DropDownListFor(model => model.LevelId,
(
from choice in
(from p in Model.Defaults
group p by new { p.LevelId, p.LevelDescription } into c
select new { c.Key.LevelId, c.Key.LevelDescription })
select new SelectListItem
{
Text = choice.LevelDescription,
Value = choice.LevelId.ToString(),
Selected = false
}))
jscript
$("#LevelId").change(function() {
var clientId = @Model.ClientId;
var compareDate = $("#EffectiveDate").val();
var level = $(this).val();
var params = $.param({ clientId: clientId, compareDate: compareDate, level : level });
var link = '@Url.Action("Create", "Choices")'; //"\\Choices\\RefreshView";
var url = link + "?" + params;
document.location = url;
});
控制器根据传入的参数设置ViewBag.Level
I have a dropdown list that reloads the page when its selected value is changed. This works and renders as expected. The only problem is that the dropdown list goes back to the default value. How can I change the the default value to match ViewBag.Value?
@Html.DropDownListFor(model => model.LevelId,
(
from choice in
(from p in Model.Defaults
group p by new { p.LevelId, p.LevelDescription } into c
select new { c.Key.LevelId, c.Key.LevelDescription })
select new SelectListItem
{
Text = choice.LevelDescription,
Value = choice.LevelId.ToString(),
Selected = false
}))
jscript
$("#LevelId").change(function() {
var clientId = @Model.ClientId;
var compareDate = $("#EffectiveDate").val();
var level = $(this).val();
var params = $.param({ clientId: clientId, compareDate: compareDate, level : level });
var link = '@Url.Action("Create", "Choices")'; //"\\Choices\\RefreshView";
var url = link + "?" + params;
document.location = url;
});
the controller sets ViewBag.Level based on the param passed in
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向视图模型添加一个属性,该属性将是
IEnumerable
,您将像在控制器中一样对其进行初始化。然后只需将 LevelId 属性设置为您想要预先选择的值:然后在您的视图中:
You could add a property to your view model which will be an
IEnumerable<SelectListItem>
which you will initialize like that in your controller. Then just set the LevelId property to a value you would like to preselect:and then in your view: