Issue with Dropdown Pre-selection in ASP.NET Core MVC with Chosen.js Plugin

Problem

I’m working on an ASP.NET Core MVC application where I need to pre-select values in dropdown lists based on data from the model. I’m using the Chosen.js plugin to enhance the dropdowns. However, the dropdowns are not showing the pre-selected values when the page loads.

Code Overview

Here’s a snippet of my view code:

@for (int i = 0; i < Model.Cards_BR.Count; i++)
{
    <tr id="row-@i">
        <td data-label="Nazwa projektu">
            <select id="ProjectName-@i" name="Cards_BR[@i].ProjectId" class="form-control">
                <option value="">Wybierz projekt</option>
                @foreach (var project in Model.Projects)
                {
                    <option value="@project.Id">@project.Name</option>
                }
            </select>
        </td>
        <!-- Similar structure for other dropdowns -->
    </tr>
}


I’m using JavaScript to set the selected values:


function setSelectedValues(index, projectId, qualificationId, phaseId, categoryId) {
    const projectNameDropdown = document.getElementById(`ProjectName-${index}`);
    const qualificationDropdown = document.getElementById(`ActivityQualification-${index}`);
    const phaseDropdown = document.getElementById(`ProjectPhase-${index}`);
    const categoryDropdown = document.getElementById(`CategoryOfActivities-${index}`);

    if (projectNameDropdown) {
        projectNameDropdown.value = projectId;
    }

    if (qualificationDropdown) {
        qualificationDropdown.value = qualificationId;
    }

    if (phaseDropdown) {
        phaseDropdown.value = phaseId;
    }

    if (categoryDropdown) {
        categoryDropdown.value = categoryId;
    }

    // Initialize Chosen after setting the values
    $(`#ProjectName-${index}`).chosen();
    $(`#ActivityQualification-${index}`).chosen();
    $(`#ProjectPhase-${index}`).chosen();
    $(`#CategoryOfActivities-${index}`).chosen();
}

The setSelectedValues function is called on DOMContentLoaded:

document.addEventListener("DOMContentLoaded", function () {
    setSelectedValues(@i, "@Model.Cards_BR[i].ProjectId", "@Model.Cards_BR[i].QualificationId", "@Model.Cards_BR[i].PhaseId", "@Model.Cards_BR[i].CategoryId");
});


Logs Observed

I added logging to the setSelectedValues function to track the values being set, and the console output shows the following:

Initializing values for row: 0
Project ID: 8, Qualification ID: 7, Phase ID: 21, Category ID: 39
Project dropdown value set to: 8
Qualification dropdown value set to: 7
Phase dropdown value set to: 21
Category dropdown value set to: 39

What I’ve Tried

  1. Ensured the model data is correct and contains the expected values.

  2. Verified that the setSelectedValues function is being called and that the correct values are being passed to it.

  3. Initialized Chosen.js after setting the values to ensure the plugin is not overriding the selection.

  4. Checked the JavaScript console for errors and confirmed that the correct values are being set in the function.

Current Behavior

Despite the values being correctly set in the JavaScript function and confirmed in the logs, the dropdowns do not display the selected values in the UI. They always show the default option (“Wybierz projekt,” “Wybierz kwalifikację,” etc.), rather than the pre-selected values from the model.

Model structure

public class ParentModel
{
    public List<Cards_BR> Cards_BR { get; set; } = new List<Cards_BR>();
    public List<Project> Projects { get; set; } = new List<Project>();
    public List<Qualification> Qualifications { get; set; } = new List<Qualification>();
    public List<Phase> Phases { get; set; } = new List<Phase>();
    public List<Category> Categories { get; set; } = new List<Category>();
}

public class Cards_BR
{
    public int ProjectId { get; set; }
    public Project Project { get; set; }

    public int QualificationId { get; set; }
    public Qualification Qualification { get; set; }

    public int PhaseId { get; set; }
    public Phase Phase { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Project
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Qualification
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Phase> Phases { get; set; } = new List<Phase>();
}

public class Phase
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int QualificationId { get; set; }
    public Qualification Qualification { get; set; }
    public ICollection<Category> Categories { get; set; } = new List<Category>();
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int PhaseId { get; set; }
    public Phase Phase { get; set; }
}


Question

What could be causing the dropdowns to not show the pre-selected values despite the logs showing that the correct values are being set? How can I ensure that the selected values are displayed correctly in the UI?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật