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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:42:42+00:00 2026-05-16T10:42:42+00:00

I am receiving this error: The ViewData item that has the key ‘DepartmentId’ is

  • 0

I am receiving this error:

The ViewData item that has the key ‘DepartmentId’ is of type ‘System.Int32’ but must be of type ‘IEnumerable’.

with the following set up. I am not sure how to resolve it. The error is happening in the Model View code. This line: public void MapTo(Person domainModel). I am using AutoMapper to map ViewModel back to DomainModel (reversing the initial mapping of DomainModel to ViewModel).

Domain model (using LINQ to SQL, so this is a partial class):

public partial class Person { }

// Validation rules
public class Person_Validation
{
    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public object PersonId { get; set; }

    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public object DepartmentId { get; set; }

    [DisplayName("Employee Name")]
    [Required(ErrorMessage = "Employee Name is required")]
    [StringLength(50, ErrorMessage = "Employee Name cannot be more than 50 characters")]
    public object Name { get; set; }

    [HiddenInput(DisplayValue = false)]        
    public object Active { get; set; }

    [HiddenInput(DisplayValue = false)]
    public object DateAdded { get; set; }

    [HiddenInput(DisplayValue = false)]
    public object DateDeleted { get; set; }

    public object Department { get; set; }     
}

This is my Model View:

public class PersonViewModel
{       
    public object PersonId { get; set; }

    public object DepartmentId { get; set; }

    public object Name { get; set; }

    public object Active { get; set; }

    public object DateAdded { get; set; }

    public object DateDeleted { get; set; }    

    public object DepartmentName { get; set; }

    //helper method
    public void MapTo(Person domainModel)
    {
        Mapper.Map(this, domainModel);
    }
}

Controller Class Code:

[HttpPost]        
public ActionResult Edit(PersonViewModel viewModel)
{
    var domainModel = new Person();                                       
    try                    
    {                      
        viewModel.MapTo(domainModel);
        UpdateModel(domainModel);
        _personRepository.Save();
        return RedirectToAction("Index", "Person");
    }                      
    catch                  
    {                      
        return View(viewModel);
    }                      
} 

And my View HTML code:

<div class="editor-field">                
<%: Html.DropDownList("DepartmentId", (IEnumerable<SelectListItem>)ViewData["DepartmentList"])%>                                     
<%: Html.ValidationMessageFor(model => model.DepartmentId) %>
</div>
  • 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-16T10:42:43+00:00Added an answer on May 16, 2026 at 10:42 am

    you’re not really following the best practices of developing a mvc application.

    About the error:
    the Html.DropDownList looks for Data of type IEnumerable<SelectListItem> in the model but it finds an int instead (DepartmentId)

    your ViewModel should not have the MapTo method, it breaks the single responsibility principle

    in your action method you don’t do any server side validation like:

    if(!ModelState.IsValid)
    {
       //rebuild the viewmodel and return the view
    }
    

    catching everything in the action is also not necessary (and bad)
    you do this in Global.asax Application_Error instead

    attributes like HiddenInput, ScaffoldColumn, Validation and anything else UI related should be on you ViewModel not in your domain model


    for a good sample of using viewmodels & validation & mapping between entity <-> viewmodel I recommend you to look at the Samples solution from here

    I did this sample and it’s main purpose is to demonstrate the usage of ValueInjecter (mapping technology)

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

Sidebar

Related Questions

I am not sure what the problem is but I keep receiving this error
I am currently receiving this error: Validation of viewstate MAC failed. If this application
A little help needed. I'm receiving an xml file similar to this: <?xml version=1.0
I'm receiving Package Load Failure error when I open VS 2005 after I installed
I'm receiving feedback from a developer that The only way visual basic (6) can
When receiving a bug report or an it-doesnt-work message one of my initials questions
I am receiving a 3rd party feed of which I cannot be certain of
My application is receiving email through SMTP server. There are one or more attachments
I am receiving dynamic xml where I won't know the attribute names, if you'll
I am receiving a large list of current account numbers daily, and storing them

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.