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 8909249
In Process

The Archive Base Latest Questions

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

I have a working scenario – My viewmodel contains a list of Employee objects.

  • 0

I have a working scenario –
My viewmodel contains a list of Employee objects.

List<Employee> Employees;

I populate this list by the following:

Employees = EmployeeService.GetAll().ToList();

This works fine. I can view the employees, update their data, post back and save to db.

However, when I try and sort the list of Employees in the viewmodel before sending to the view by replacing the code above with:

Employees = EmployeeService.GetAll().OrderBy(e=>e.Name).ToList();

The view is populated nicely with the ordered employee details. Unfortunately when I post back this viewmodel to the controller, the viewmodel.Employees is null / empty.

I’d appreciate any help if anyone has any ideas what I might be doing wrong here or why this is happening only when I sort.

Regards

Edit–

public ActionResult Index()
{
    EmployeesViewModel _viewModel = new EmployeesViewModel();
    return View(_viewModel);
}

[HttpPost]
public ActionResult Index(EmployeesViewModel viewModel)
{
    // HERE ** - viewModel.Employees is NULL
    EmployeesService.UpdateAllEmployees(viewModel);
    return View(viewModel);
}

Edit- Sample Markup —

for (int i = 0; i < Model.Employees.Count(); i++)
{
    @Html.HiddenFor(e => e.Employees[i].Id)
    @Html.HiddenFor(e => e.Employees[i].Name)
    <table>
        <tr>
            <td style = "width: 125px">
                @Model.Employees[i].Name
            </td>
            <td style = "width: 125px">
                @Html.CheckBoxFor(e => e.Employees[i].IsActive)
            </td>
        </tr>
    </table>
}    

Edit – Class Details

public class Employee
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string InitialCode { get; set; }
    public bool IsActive { get; set; }

    public Employee()
    {

    }

}


public class EmployeesViewModel
{
    public List<Employee> Employees { get; set; }
    private readonly EmployeesService EmployeesService;

    public EmployeesViewModel()
    {
        Employees = new List<Employee>();
        EmployeesService = new EmployeesService();
        // Employees = EmployeesService.GetAll().ToList(); //THIS WORKS
        Employees = EmployeesService.GetAll().OrderBy(e=>e.Name).ToList();
    }
}

EmployeesService simply calls the DB Context and returns all records.

  • 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-15T03:23:00+00:00Added an answer on June 15, 2026 at 3:23 am

    Your HTML form elements need to be rendered in a way that the DefaultModelBinder can correctly parse out the collection. Check out this: https://github.com/danludwig/BeginCollectionItem .

    Also read this and this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a contrived example, but consider a scenario where employees have working locations,
I already have the following scenario working: Assembly A.exe : Calling WinForms application. Assembly
Have been working on this question for a couple hours and have come close
Here is my scenario. I have a working login form that functions correctly. I
This is one of the problem i have been working from a long time.This
I have this scenario: One client sends a message into a msmq queue instance
I am working on a scenario in Prolog (eclipse) wherein I need a list
Here is the scenario. I have a working VSX package Proof of Concept that
I'm new to RX, and I have my desired scenario working well, but it
I have been working on this program for a while and I finally got

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.