根据viewbag设置默认下拉默认值

发布于 2024-12-27 22:23:37 字数 1360 浏览 1 评论 0原文

我有一个下拉列表,当其选定值更改时,它会重新加载页面。这可以按预期工作并呈现。唯一的问题是下拉列表返回到默认值。如何更改默认值以匹配 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2025-01-03 22:23:37

您可以向视图模型添加一个属性,该属性将是 IEnumerable,您将像在控制器中一样对其进行初始化。然后只需将 LevelId 属性设置为您想要预先选择的值:

public ActionResult Foo(string level)
{
    var model = new MyViewModel();
    var values = 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 {
             Text = choice.LevelDescription,
             Value = choice.LevelId.ToString()
         }
    );
    model.Items = new SelectList(values, "Text", "Value");
    model.LevelId = level;
    return View(model);
}

然后在您的视图中:

@Html.DropDownListFor(model => model.LevelId, Model.Items)

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:

public ActionResult Foo(string level)
{
    var model = new MyViewModel();
    var values = 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 {
             Text = choice.LevelDescription,
             Value = choice.LevelId.ToString()
         }
    );
    model.Items = new SelectList(values, "Text", "Value");
    model.LevelId = level;
    return View(model);
}

and then in your view:

@Html.DropDownListFor(model => model.LevelId, Model.Items)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文