Question

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

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top