开发者

MVC - No Viewdata item with key of type 'IEnumerable<SelectListItem>'

开发者 https://www.devze.com 2022-12-18 04:16 出处:网络
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.

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 A开发者_如何学GoctionResult 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


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

精彩评论

暂无评论...
验证码 换一张
取 消