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

  • SEARCH
  • Home
  • 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 8785469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:22:10+00:00 2026-06-13T21:22:10+00:00

I’m displaying a list of users. Each user record has a TrainingEvent object property

  • 0

I’m displaying a list of users. Each user record has a TrainingEvent object property (from a choice of many TrainingEvents). I’ve created a viewmodel to include the User list and the TrainingEvents list.

ViewModel:

public class RegistrationViewModel
{
  public IEnumerable<Domain.Entities.User> Users { get; set; }
  public IEnumerable<SelectListItem> TrainingEvents { get; set; }
}

(note: the Domain.Entities.User contains a TrainingEvent object property)

Controller:

public ActionResult Dealership(int id)
{
  var model = new RegistrationViewModel
    {
      Users = repository.Find
             .Where(u => u.Dealer.DealerId == id).OrderBy(u => u.LastName),
                 TrainingEvents = repository.TrainingEvents.ToList()
                                  .Select(x => new SelectListItem
                                          {
                                            Text = x.Date.ToString() + " - " + x.LocationName,
                                            Value = x.TrainingEventId.ToString()
                                          })
    };

  return View("East", model);
}

Binding in View:

@foreach(Company.Domain.Entities.User usr in Model.Users)
  {
     <tr>
         <td>@usr.LastName</td>
         <td>@usr.FirstName</td>
         <td>@usr.JobDescription</td>
         <td>@Html.DropDownListFor(m => m.Users.FirstOrDefault(u => u.UserId == usr.UserId).TrainingEvent, Model.TrainingEvents)</td>
     </tr>
}

The users are listed and each user row has a dropdown which populates with TrainingEvents. However, the value previously saved in the user’s TrainingEvent object isn’t selected. Any ideas as why not?

  • 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-13T21:22:11+00:00Added an answer on June 13, 2026 at 9:22 pm

    You are incorrectly using the DropDownListFor helper. This helper expects to be passed as first argument a simple lambda expression containing at most a member access. In your example you are attempting to construct some complex lambda expression using things like FirstOrDefault extension methods which is not supported. Also it is not the responsibility of the view to be fetching some data from the model. It’s the responsibility of the controller to populate a suitable view model for the view and pass it for consumption

    So I would recommend you to use a real view model reflecting the requirements of your view:

    public class UserViewModel
    {
        public int TrainingEventId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string JobDescription { get; set; }
    }
    
    public class RegistrationViewModel
    {
        public IList<UserViewModel> Users { get; set; }
        public IEnumerable<SelectListItem> TrainingEvents { get; set; }
    }
    

    which will be populated in your controller action:

    public ActionResult Dealership(int id)
    {
        var users = repository
            .Find
            .Where(u => u.Dealer.DealerId == id)
            .OrderBy(u => u.LastName)
            .Select(u => new UserViewModel
            {
                TrainingEventId = u.TrainingEvent.TrainingEventId,
                FirstName = u.FirstName,
                LastName = u.LastName,
                JobDescription = u.JobDescription
            })
            .ToList();
    
        var trainingEvents = repository
            .TrainingEvents
            .ToList()
            .Select(x => new SelectListItem
            {
                Text = x.Date.ToString() + " - " + x.LocationName,
                Value = x.TrainingEventId.ToString()
            });
    
        var model = new RegistrationViewModel
        {
            TrainingEventIds = TrainingEventIds,
            TrainingEvents = trainingEvents
        };
    
        return View("East", model);
    }
    

    and finally in your view bind the DropDownListFor helper to the corresponding value in the view model:

    @for(var i = 0 ; i < Model.TrainingEventIds; i++)
    {
        <tr>
            <td>@Html.DisplayFor(m => m.Users[i].LastName)</td>
            <td>@Html.DisplayFor(m => m.Users[i].FirstName)</td>
            <td>@Html.DisplayFor(m => m.Users[i].JobDescription)</td>
            <td>
                @Html.DropDownListFor(
                    m => m.Users[i].TrainingEventId, 
                    Model.TrainingEvents
                )
            </td>
        </tr>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from

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.