Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:22:20+00:00 2026-06-15T13:22:20+00:00

Follow up question to : Populate a list property from delimited string The problem

  • 0

Follow up question to :
Populate a list property from delimited string

The problem I’m having is the DropDown boxes in my Edit view are not retaining/selecting the the proper item based on the AssignedUserID property within BugAssignment model.

Models:

public class BugAssignment
{
    public int BugAssignmentID { get; set; }
    public int BugNumber { get; set; }
    public int? AssignedUserID { get; set; }
    public virtual User AssignedUser { get; set; }
    public virtual Status Status { get; set; }
    public IEnumerable<SelectListItem> Users { get; set; }
}

public class BugAssignmentList
{
    public BugAssignmentList()
    {
        BugAssignments = new List<BugAssignment>();
    }
    public int BugAssignmentListID { get; set; }
    public string Name { get; set; }
    public List<BugAssignment> BugAssignments { get; set; }
}

Edit/Details ViewModel

public class BugAssignmentListDetailsViewModel
{
    public int BugAssignmentListID { get; set; }
    public string Name { get; set;}
    public List<BugAssignment> BugAssignments { get; set; }
}

I have the following two methods in my controller:

    public ActionResult Edit(int id)
    {
        var balData = db.BugAssignmentLists.Include(bn => bn.BugAssignments).ToList();
        //this is a reference to the bug assignment list object whose ID was passed in
        BugAssignmentList bugAssignmentList = balData.Where(b => b.BugAssignmentListID == id).FirstOrDefault();
        //loop through the bug assignment list and for each bug assignment
        foreach (BugAssignment b in bugAssignmentList.BugAssignments)
        {
            //create the select list for each BugAssignment object
            b.Users = users.Select(u => new SelectListItem
            {
                Value = u.UserID.ToString(),
                Text = u.FullName,
                Selected = u.UserID == b.AssignedUserID //<<THIS IS WHERE IT IS MESSING UP
            });

        }
        BugAssignmentListDetailsViewModel detailsVM = new BugAssignmentListDetailsViewModel
        {
            BugAssignmentListID = id
        };
        if (bugAssignmentList != null)
        {
            detailsVM.BugAssignments = bugAssignmentList.BugAssignments;
            detailsVM.Name = bugAssignmentList.Name;
        }
        return View(detailsVM);
    }

    // POST
    [HttpPost]
    public ActionResult Edit(BugAssignmentListDetailsViewModel viewModel)
    {
        var balData = db.BugAssignmentLists.Include(bn => bn.BugAssignments).ToList();
        var users = db.Users.ToList();
        BugAssignmentList bugAssignmentList = balData.Where(b => b.BugAssignmentListID == viewModel.BugAssignmentListID).FirstOrDefault();
        bugAssignmentList.Name = viewModel.Name;
        bugAssignmentList.BugAssignments = viewModel.BugAssignments;

        if (ModelState.IsValid)
        {
            UpdateModel(bugAssignmentList, "BugAssignmentList");
            db.Entry(bugAssignmentList).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(bugAssignmentList);
    }

View:

<table>    
<thead>
    <th>
        Bug Number
    </th>
    <th>
        Assigned User
    </th>
</thead>
    @for(int i=0; i<Model.BugAssignments.Count(); i++)
    {
        <tr>
            <td style="text-align: center;">
                @Html.EditorFor(modelItem => Model.BugAssignments[i].BugNumber)
            </td>
            <td>
                @Html.DropDownListFor(modelItem => Model.BugAssignments[i].AssignedUserID, Model.BugAssignments[i].Users, "-- Select User --")
                @Html.HiddenFor(modelItem => Model.BugAssignments[i].BugAssignmentID)
            </td>
        </tr>
    }
</table>

The POST is working fine. Looking in the database, each BugAssignment in the BugAssignments collection within BugAssignmentList is updating with the proper AssignedUserID from the dropdown box in my Edit view, so it’s not the POST method.

The problem is in the Edit(int id). What’s happening is very odd. I’ve put a breakpoint right before the foreach loop and I looked inside of bugList.BugAssignments. All the AssignedUserID’s are correct, but after the loop completes, and I inspect the list again, all the SelectListItems in BugAssignment.Users have the same User selected.

What is wrong with the following code that would cause this?

    foreach (BugAssignment b in bugAssignmentList.BugAssignments)
    {
        //create the select list for each BugAssignment object
        b.Users = users.Select(u => new SelectListItem
        {
            Value = u.UserID.ToString(),
            Text = u.FullName,
            Selected = u.UserID == b.AssignedUserID //<<THIS IS WHERE IT IS MESSING UP
        });

    }

Also, as a side note: I want the interface to look something like this (which is why I need the SelectList to be on the BugAssignment model, unless there is an easier way. I don’t want to write a BugAssignment controller/view I want someone to be able to quickly assign users to bug numbers like the following:
enter image description here

Here is another screenshot that better shows what is happening:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:22:21+00:00Added an answer on June 15, 2026 at 1:22 pm

    I believe this is a closure issue. Try creating a local copy of b.assignedUserID:

    foreach (BugAssignment b in bugAssignmentList.BugAssignments)
    {
        var assignedUserID = b.AssignedUserID;
    
        //create the select list for each BugAssignment object
        b.Users = users.Select(u => new SelectListItem
        {
            Value = u.UserID.ToString(),
            Text = u.FullName,
            Selected = (u.UserID == assignedUserID),
        });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Follow up question from Multi-core usage, threads, thread-pools . Are threads moved from one
This is a follow up question from Calling constructor in return statement . This
This is a follow-on question from the one I asked here . Can constraints
As a follow on from this question I'm building a custom server control to
This is a follow up question from Team Foundation Server: Getting the Changeset Id
Follow-on to an earlier question: Force SET IDENTITY_INSERT to take effect faster from MS
Follow on from a previous question One first screen activity I am clicking a
Follow up question from What is a "rooted reference"? consider this code, for a
Is this code possible? A stupid question but I am having a problem understanding
A follow-up question to my answer about editing GitHub pull-requests asked whether editing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.