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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:49:17+00:00 2026-06-12T13:49:17+00:00

I use MVC 3 in ASP.Net my web app is design with ViewModel and

  • 0

I use MVC 3 in ASP.Net my web app is design with ViewModel and ViewModel builder.

I use a Builder class to populate some data in a ViewModel. In my case I have a Create View a DropDownList, with this code work fine. My problem is when trying to create an Edit View, I receive this error:

   {"The ViewData item that has the key 'CandidateId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'."}

My idea it would be to populate a DropDownList with some value but have pre-selected one as for databse record.

So How to display a DropDownList in a Edit View with selected a value from a DataBase?

VIEW

    <div class="editor-label">
        @Html.LabelFor(model => model.CandidateId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(x => x.CandidateId, Model.CandidatesList, "None")
    </div>

VIEW MODEL

    public Nullable<int> CandidateId { get; set; }
    public IEnumerable<SelectListItem> CandidatesList;

VIEW MODEL BUILDER

// We are creating the SelectListItem to be added to the ViewModel
        eventEditVM.CandidatesList = serviceCandidate.GetCandidates().Select(x => new SelectListItem
            {
                Text = x.Nominative,
                Value = x.CandidateId.ToString()
            });
  • 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-12T13:49:18+00:00Added an answer on June 12, 2026 at 1:49 pm

    The reason for this error is because in your [HttpPost] action you forgot to repopulate the CandidatesList property on your view model from the database.

    [HttpPost]
    public ActionResult Edit(EventEditVM model)
    {
        if (ModelState.IsValid)
        {
            // the model is valid => do some processing here and redirect
            // you don't need to repopulate the CandidatesList property in 
            // this case because we are redirecting away
            return RedirectToAction("Success");
        }
    
        // there was a validation error => 
        // we need to repopulate the `CandidatesList` property on the view model 
        // the same way we did in the GET action before passing this model
        // back to the view
        model.CandidatesList = serviceCandidate
            .GetCandidates()
            .Select(x => new SelectListItem
            {
                Text = x.Nominative,
                Value = x.CandidateId.ToString()
            });
    
        return View(model);
    }
    

    Don’t forget that only the selected value of the dropdown list is sent to the server when you submit the form. The CandidatesList collection property will be null inside your POST controller action because its values were never sent. So if you intend to redisplay the same view you need to initialize this property because your view depends on it.

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

Sidebar

Related Questions

I have an ASP.NET MVC web app whose controllers use WCF to call into
so i have a multi-tier web app in asp.net mvc i use the asp.net
We have an ASP.NET MVC web app which allows users to publish messages onto
I have developed a web app in ASP.NET MVC which we are using internally.
I have put together a simple ASP.NET MVC 3 test web app that uses
Im trying switch the Visual Studio asp.net mvc web app template to use MySql
We've an ASP.Net MVC2 web app (SQL 2008 in backend). We use Data Annotations
Background I have a page on my ASP.NET MVC web app for users to
I have an Asp.Net MVC 3 mobile web app and get requests fail with
I have an ASP.NET MVC web app used to monitor the state of my

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.