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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:52:39+00:00 2026-05-13T14:52:39+00:00

I’m new to MVC and ASP.NET and am looking to learn a bit, so

  • 0

I’m new to MVC and ASP.NET and am looking to learn a bit, so I’m trying to make some trivial applications to learn the ins and outs.

Well, I’m trying to make a dropdown box show a list of books where it would show the title of the book but post the book_id [primary key]

The error I get is:: There is no ViewData item with the key ‘book_id’ of type ‘IEnumerable’.

Here is what is in my view:

    <p>
        <label for="book_id">Book:</label>

        <%= Html.DropDownList("book_id" , (IEnumerable<SelectListItem>)ViewData["Books"]) %> 
        <%= Html.ValidationMessage("book_id", "*") %>
    </p>

Here is what’s in my controller

        // GET: /Home/Create
//This is the form creation. 
[Authorize]
public ActionResult Create()
{
    this.ViewData["Books"] =
        new SelectList(_entities.BookSet.ToList(), "book_id", "Title");
    return View();
} 

//
// POST: /Home/Create
//This sends it to the DB
[AcceptVerbs(HttpVerbs.Post) , Authorize]
public ActionResult Create([Bind(Exclude="problem_id")] Problem inProblem)
{
    try
    {
        // TODO: Add insert logic here
        Models.User user = getUser(User.Identity.Name);
        if (user != null)
        {
            inProblem.user_id = user.user_id;
        }
        _entities.AddToProblemSet(inProblem);
        _entities.SaveChanges();

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

And my Books table looks like this

        book_id
        title
        publisher
        language
        isbn

I’m assuming this is a trivial newbie mistake; but I’m not having much luck figuring it out. Any help would be great

  • 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-13T14:52:39+00:00Added an answer on May 13, 2026 at 2:52 pm

    Easier solution is to just name the ViewData element the same as the drop down list:

    this.ViewData["book_id"] = new SelectList(_entities.BookSet.ToList(), "book_id", "Title"); 
    

    this will automatically bind with:

    <%= Html.DropDownList("book_id") %>
    

    You will also need to populate the ViewData[“book_id”] in the Post version of Create if you want to display the view again (as you do in the catch).

    [AcceptVerbs(HttpVerbs.Post) , Authorize] 
    public ActionResult Create([Bind(Exclude="problem_id")] Problem inProblem) 
    { 
        try 
        { 
            // TODO: Add insert logic here 
            Models.User user = getUser(User.Identity.Name); 
            if (user != null) 
            { 
                inProblem.user_id = user.user_id; 
            } 
            _entities.AddToProblemSet(inProblem); 
            _entities.SaveChanges(); 
    
            return RedirectToAction("Index"); 
        } 
        catch 
        { 
            this.ViewData["Books"] = new SelectList(_entities.BookSet.ToList(), "book_id", "Title"); 
            // ViewData["book_id"] with example above.
    
            return View(); 
        } 
    } 
    

    Note: The this.ViewData[“Books”] above should really be done within the catch – its just there for demonstration on whats missing.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.