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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:59:06+00:00 2026-05-19T12:59:06+00:00

I am trying to build a Dropdownlist, but battling with the Html.DropDownList rendering. I

  • 0

I am trying to build a Dropdownlist, but battling with the Html.DropDownList rendering.

I have a class:

public class AccountTransactionView
{
    public IEnumerable<SelectListItem> Accounts { get; set; }
    public int SelectedAccountId { get; set; }
}

That is basically my view model for now. The list of Accounts, and a property for returning the selected item.

In my controller, I get the data ready like this:

public ActionResult AccountTransaction(AccountTransactionView model)
{
    List<AccountDto> accounts = Services.AccountServices.GetAccounts(false);

    AccountTransactionView v = new AccountTransactionView
    {
        Accounts = (from a in accounts
                    select new SelectListItem
                    {
                        Text = a.Description,
                        Value = a.AccountId.ToString(),
                        Selected = false
                    }),
    };

    return View(model);
}

Now the problem:

I am then trying to build the Drop down in my view:

<%=Html.DropDownList("SelectedAccountId", Model.Accounts) %>

I am getting the following error:

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

Why would it want me to return the whole list of items? I just want the selected value. How should I be doing this?

  • 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-19T12:59:07+00:00Added an answer on May 19, 2026 at 12:59 pm

    You have a view model to which your view is strongly typed => use strongly typed helpers:

    <%= Html.DropDownListFor(
        x => x.SelectedAccountId, 
        new SelectList(Model.Accounts, "Value", "Text")
    ) %>
    

    Also notice that I use a SelectList for the second argument.

    And in your controller action you were returning the view model passed as argument and not the one you constructed inside the action which had the Accounts property correctly setup so this could be problematic. I’ve cleaned it a bit:

    public ActionResult AccountTransaction()
    {
        var accounts = Services.AccountServices.GetAccounts(false);
        var viewModel = new AccountTransactionView
        {
            Accounts = accounts.Select(a => new SelectListItem
            {
                Text = a.Description,
                Value = a.AccountId.ToString()
            })
        };
        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 a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Im trying to build a kd-tree for searching through a set of points, but
Im trying to build and automated log in using Googles authSub, but I need
I'm trying to build a custom dropdownlist which show/hide a second set of dropdowns
Trying to build a linq query based on parameters a user selects (reporting) but
iam trying to build a multidimensional array. public function saveRateTemplateData($RateTemplateInfo) { $RateTemplateID = $RateTemplateInfo['id'];
Upon trying to build even the simplest Metro application in Visual Studio, I get
Trying to build a legacy code in VS2005 and get errors in VC header
Im trying to build a simple, lightweight product viewer using jquery. I have a
I am trying to build a dropdown list for a winform interop, and I

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.