You may want to add your SelectList object to the ViewData as opposed to the ViewBag:
In your controller:
ViewData["JobCodeType"] = items;
If it initially populates and then you receive this error - it could be that the ViewData / ViewBag contents are being lost after any refreshing / postbacks, you may want to consider using a less volatile method of storage (such as the Session).
You can find a tutorial on basic MVC View Population (and MVC in general)here.
Another thing to look into would be to try to access the variable within the top of your View, similar to your title, and see if it throws any issues there. (Perhaps check the count of it?)
when using DropDownList and ListBox helpers using name and without specifying Items like this :-
@Html.DropdownList("Categories")
that means ,The DorpDownlist helper should check ViewData to get the key with the name "Categories" and this key should implement IEnumerable<SelectListItem> .
and if you talk about List<SelectListItem> , we will find that The "List" Class already Implement IEnumerable.
all of this means that Code should work .
@mozart_azul , try to check your code a gain , and remmber that ViewData related to one View :" it used to transfer data between Action Method and Razor/aspx View "
I understand that. However - I was just offering some alternative solutions that might help him resolve his issue
(or at least attempt to debug the issue), as it seems that the proper routes for implementing this haven't exactly been working very well
(or as intended).
@mozart -
Another important thing is to ensure that you don't have any type of spelling errors lurking around.
I do not understand why it is not seeing the name key “JobCodeType” coming from ViewData.
If you see my code, it looks correct. The only thing I did not do (comparing against your code), is that I kept the name “SelectJobCode” for the Action Result instead of “Index” because “Index” it is been used already, but that it should not matter correct?.
This is the code for the full Controller in case that something it is interfering with the ViewData.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcLog.Models;
namespace MvcLog.Controllers
{
public class LogsController : Controller
{
private LogDBContext db = new LogDBContext();
// GET: /Logs/
public ViewResult Index()
{
return View(db.Logs.ToList());
}
// GET: /Logs/Details/5
public ViewResult Details(int id)
{
Log log = db.Logs.Find(id);
return View(log);
}
// GET: /Logs/Create
public ActionResult Create()
{
return View();
}
// POST: /Logs/Create
[HttpPost]
public ActionResult Create(Log log)
{
if (ModelState.IsValid)
{
db.Logs.Add(log);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(log);
}
// GET: /Logs/Edit/5
public ActionResult Edit(int id)
{
Log log = db.Logs.Find(id);
return View(log);
}
public ActionResult SelectJobCode()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Community", Value = "0"});
items.Add(new SelectListItem { Text = "In-House", Value = "1", Selected = true });
items.Add(new SelectListItem { Text = "Vacation", Value = "2" });
items.Add(new SelectListItem { Text = "Sick", Value = "3" });
items.Add(new SelectListItem { Text = "Absent", Value = "4" });
items.Add(new SelectListItem { Text = "Not Scheduled", Value = "5" });
ViewData["JobCodeType"] = items;
return View();
}
// POST: /Logs/Edit/5
[HttpPost]
public ActionResult Edit(Log log)
{
if (ModelState.IsValid)
{
db.Entry(log).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(log);
}
// GET: /Logs/Delete/5
public ActionResult Delete(int id)
{
Log log = db.Logs.Find(id);
return View(log);
}
// POST: /Logs/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Log log = db.Logs.Find(id);
db.Logs.Remove(log);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
mozart_azul
Member
4 Points
25 Posts
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType'.
Apr 13, 2012 06:39 PM|LINK
Hello,
I am new to ASP.NET and trying to learn how to program with MVC.
I had created an application without any problem until I got to the point where
I want to add a dropdown list. This section has been a little confused for me and I am stuck.
I’ll appreciate any help.
Thanks in advance!!!!
Here is my code:
Controller
public ActionResult SelectJobCode()
{
List<SelectListItem> items = new List<SelectListItem>(
items.Add(new SelectListItem {Text = "Community", Value = "0"}); items.Add(new SelectListItem {Text = "In-House", Value = "1", Selected = true });
items.Add(new SelectListItem { Text = "Vacation", Value = "2" });
items.Add(new SelectListItem { Text = "Sick", Value = "3" });
items.Add(new SelectListItem { Text = "Absent", Value = "4" });
items.Add(new SelectListItem { Text = "Not Scheduled", Value = "5" });
ViewBag.JobCodeType = items;
return View();
}
VIEW
@model MvcLog.Models.Log
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Log</legend>
@* HERE IS MY PROBLEM *@
<div class="editor-label" style="float:left; margin-right:24px; color: #0000FF">
<br />
@Html.LabelFor(model => model.Job_code)
</div>
<div class="editor-label" style="float:left">
<br />
@Html.DropDownList("JobCodeType")
@Html.ValidationMessageFor(model => model.Job_code)
</div>
<div style="clear: both">
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Ahmed Moosa
Star
7784 Points
1232 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 06:50 PM|LINK
try to replace this line :-
ViewBag.JobCodeType = items;
with this line :
ViewData["JobCodeType"] = items ;
MCC - MCPD -MCTS
Rion William...
All-Star
27484 Points
4548 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 06:51 PM|LINK
You may want to add your SelectList object to the ViewData as opposed to the ViewBag:
In your controller:
If it initially populates and then you receive this error - it could be that the ViewData / ViewBag contents are being lost after any refreshing / postbacks, you may want to consider using a less volatile method of storage (such as the Session).
You can find a tutorial on basic MVC View Population (and MVC in general) here.
mozart_azul
Member
4 Points
25 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 06:56 PM|LINK
Ahmed,
I 'm still getting the same error message
Ahmed Moosa
Star
7784 Points
1232 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 07:00 PM|LINK
it should work , my code like this :
public ActionResult Index() { List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Community", Value = "0" }); items.Add(new SelectListItem { Text = "In-House", Value = "1", Selected = true }); items.Add(new SelectListItem { Text = "Vacation", Value = "2" }); items.Add(new SelectListItem { Text = "Sick", Value = "3" }); items.Add(new SelectListItem { Text = "Absent", Value = "4" }); items.Add(new SelectListItem { Text = "Not Scheduled", Value = "5" }); ViewData["JobCodeType"] = items; return View(); }and put this line in the View
@Html.DropDownList("JobCodeType")
try to remove whitespaces
MCC - MCPD -MCTS
mozart_azul
Member
4 Points
25 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 07:15 PM|LINK
It does not work
I have no spaces, exactly the same that sent.
Same message.
Rion William...
All-Star
27484 Points
4548 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 07:42 PM|LINK
I read about a similar issue of the DropDownList expecting a SelectList as opposed to a List<SelectListItem>:
StackOverflow | There is no ViewData of type "Inumer...
Another thing to look into would be to try to access the variable within the top of your View, similar to your title, and see if it throws any issues there. (Perhaps check the count of it?)
@{ ViewBag.Title = "Edit"; List<SelectListItem> yourList = ViewData["JobCodeType"] as List<SelectListItem>; }Ahmed Moosa
Star
7784 Points
1232 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 08:03 PM|LINK
@Rion Williams
when using DropDownList and ListBox helpers using name and without specifying Items like this :-
@Html.DropdownList("Categories")
that means ,The DorpDownlist helper should check ViewData to get the key with the name "Categories" and this key should implement IEnumerable<SelectListItem> .
and if you talk about List<SelectListItem> , we will find that The "List" Class already Implement IEnumerable.
all of this means that Code should work .
@mozart_azul , try to check your code a gain , and remmber that ViewData related to one View :" it used to transfer data between Action Method and Razor/aspx View "
Hope this Helps
MCC - MCPD -MCTS
Rion William...
All-Star
27484 Points
4548 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 13, 2012 08:16 PM|LINK
@Ahmed -
I understand that. However - I was just offering some alternative solutions that might help him resolve his issue (or at least attempt to debug the issue), as it seems that the proper routes for implementing this haven't exactly been working very well (or as intended).
@mozart -
Another important thing is to ensure that you don't have any type of spelling errors lurking around.
mozart_azul
Member
4 Points
25 Posts
Re: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'JobCodeType...
Apr 16, 2012 01:36 PM|LINK
Ahmed,
I have try everything but it does not work.
I do not understand why it is not seeing the name key “JobCodeType” coming from ViewData.
If you see my code, it looks correct. The only thing I did not do (comparing against your code), is that I kept the name “SelectJobCode” for the Action Result instead of “Index” because “Index” it is been used already, but that it should not matter correct?.
This is the code for the full Controller in case that something it is interfering with the ViewData.
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using MvcLog.Models; namespace MvcLog.Controllers { public class LogsController : Controller { private LogDBContext db = new LogDBContext(); // GET: /Logs/ public ViewResult Index() { return View(db.Logs.ToList()); } // GET: /Logs/Details/5 public ViewResult Details(int id) { Log log = db.Logs.Find(id); return View(log); } // GET: /Logs/Create public ActionResult Create() { return View(); } // POST: /Logs/Create [HttpPost] public ActionResult Create(Log log) { if (ModelState.IsValid) { db.Logs.Add(log); db.SaveChanges(); return RedirectToAction("Index"); } return View(log); } // GET: /Logs/Edit/5 public ActionResult Edit(int id) { Log log = db.Logs.Find(id); return View(log); } public ActionResult SelectJobCode() { List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Community", Value = "0"}); items.Add(new SelectListItem { Text = "In-House", Value = "1", Selected = true }); items.Add(new SelectListItem { Text = "Vacation", Value = "2" }); items.Add(new SelectListItem { Text = "Sick", Value = "3" }); items.Add(new SelectListItem { Text = "Absent", Value = "4" }); items.Add(new SelectListItem { Text = "Not Scheduled", Value = "5" }); ViewData["JobCodeType"] = items; return View(); } // POST: /Logs/Edit/5 [HttpPost] public ActionResult Edit(Log log) { if (ModelState.IsValid) { db.Entry(log).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(log); } // GET: /Logs/Delete/5 public ActionResult Delete(int id) { Log log = db.Logs.Find(id); return View(log); } // POST: /Logs/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Log log = db.Logs.Find(id); db.Logs.Remove(log); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } }VIEW
<div class="editor-label" style="float:left; margin-right:24px; color: #0000FF"> <br /> @Html.LabelFor(model => model.Job_code) </div> <div class="editor-label" style="float:left"> <br /> @Html.DropDownList("JobCodeType") @Html.ValidationMessageFor(model => model.Job_code) </div> <div style="clear: both"> </div>