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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:58:06+00:00 2026-05-20T10:58:06+00:00

So I have a page to edit employees. Here is my view model: public

  • 0

So I have a page to edit employees.

Here is my view model:

public class EmployeesViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int EmployeeId { get; set; }      

    [Required(ErrorMessage = "Position is required")]
    [DisplayName("Position")]
    public int EmployeeTypeId { get; set; }

    [Required(ErrorMessage = "Name is required")]
    [DisplayName("Name")]
    public string Name { get; set; }


    public IEnumerable<EmployeeType> EmployeeTypes { get; set; }
}

Here is my controller:

public class EmployeesController : Controller
{
    public ActionResult Edit(int id)
    {
        //get employee from id
        var employee = GetEmployee(id);

        if (employee != null)
        {
            var viewModel = new EmployeesViewModel
            {
                EmployeeId = employee.EmployeeID,
                EmployeeTypeId = employee.EmployeeTypeID,
                Name = employee.Name,

                EmployeeTypes = _adminRepository.GetAllEmployeeTypes(),                    
            };

            return View(viewModel);
        }

        //if no employee exists for this id, redirect to the Create page and display a friendly message
        TempData["message"] = "No employee exists with an ID of " + id + ", you can create a new employee here.";
        return RedirectToAction("Create");
    }



    [HttpPost]
    public ActionResult Edit(EmployeesViewModel viewModel)
    {
        //if editing an employee, fetch it; otherwise, create a new one
        Employee employee = GetEmployee(viewModel.EmployeeId);
        TryUpdateModel(employee);

        if (ModelState.IsValid)
        {              
            SaveEmployee(employee);
            TempData["message"] = "Employee has been saved.";
            return RedirectToAction("Details", new { id = employee.EmployeeID });
        }
        return View(viewModel);     // validation error, so redisplay same view
    }
}

And my Edit view page:

<%@ Page Title="" Language="C#" MasterPageFile="/Admin.Master" Inherits="System.Web.Mvc.ViewPage<EmployeesViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <h1>Edit Employee</h1>

    <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm("Edit", "Employees", FormMethod.Post)) { %>

        <%: Html.HiddenFor(m => m.EmployeeId)%>

        <div class="editor-label"><%: Html.LabelFor(m => m.EmployeeTypeId) %></div>
        <div class="editor-field">
            <%= Html.DropDownListFor(m => m.EmployeeTypeId, new SelectList(Model.EmployeeTypes, "EmployeeTypeID", "Position", Model.EmployeeTypeId), "- Select an Employee Type -")%>
            <%: Html.ValidationMessageFor(m => m.EmployeeTypeId)%>
        </div>

        <div class="editor-label"><%: Html.LabelFor(m => m.Name) %></div>
        <div class="editor-field">
            <%: Html.TextBoxFor(m => m.Name)%>
            <%: Html.ValidationMessageFor(m => m.Name)%>
        </div>

        <p>
            <input type="submit" value="Save" />
            <%: Html.ActionLink("Cancel", "Index") %>
        </p>

    <% } %>

</asp:Content>

After submitting my form, it fails at if (ModelState.IsValid). It tries to redisplay the view when return View(viewModel); is called, and I get this error message:

Value cannot be null.

Parameter name: items

<%= Html.DropDownListFor(m => m.EmployeeTypeId, new SelectList(Model.EmployeeTypes, “EmployeeTypeID”, “Position”, Model.EmployeeTypeId), “- Select an Employee Type -“)%>

I’m not sure why this is happening. The dropdown is loaded correctly when I navigate to the page, but not when the view is redisplayed.

Does anyone know what’s going on 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-05-20T10:58:06+00:00Added an answer on May 20, 2026 at 10:58 am

    You need to reload the EmployeeTypes property on your view model from your repository before redisplaying the view in case of error. This property is never posted so it would be always be null inside your POST action and when the view is rendered the helper would throw an exception:

    [HttpPost]
    public ActionResult Edit(EmployeesViewModel viewModel)
    {
        //if editing an employee, fetch it; otherwise, create a new one
        Employee employee = GetEmployee(viewModel.EmployeeId);
        TryUpdateModel(employee);
        if (ModelState.IsValid)
        {              
            SaveEmployee(employee);
            TempData["message"] = "Employee has been saved.";
            return RedirectToAction("Details", new { id = employee.EmployeeID });
        }
    
        // Reload employee types from repository before redisplaying the view
        viewModel.EmployeeTypes = _adminRepository.GetAllEmployeeTypes();
    
        // validation error, so redisplay same view
        return View(viewModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an edit page with url example.com/product/edit/11 Now if the validation is false
I have some CRUD routing: /news/{page} // public /news/new // private /news/{slug}/show //public /news/{slug}/edit
I have folders and pages /admin/add/index.php, /admin/edit/index.php & /admin/view/index.php which all are requiring page
I have this page :8000/edit/6/ that show a form to update an exciting model
I have a Person base class and three derivatives. On the edit page I
I have two pages, a employee edit page and a view all employe table
I have a page that lets the user edit the content of the page
I have a page where the user can edit various content using buttons and
i have a small problem, i am creating a edit page in my asp.net
In my ASP.NET MVC app, I have a fairly complex edit page which combines

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.