---
title: "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'UserName'"  
description: "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'UserName'"  
author: "Anonymous User"  
published: 2014-12-19  
updated: 2016-06-07  
canonical: https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username  
category: "asp.net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'UserName'

There is no [ViewData](https://www.mindstick.com/forum/155740/define-viewdata-in-mvc) item of type 'IEnumerable' that has the key 'UserName'. In view have 2 dropdownlists - for users and roles, after submitting i need to save role for choosen user. with role everything is ok, but trouble with user. i have list of users in list but i cant read slected for db. if use Html.TextBox("UserName") -it works, but admin will write username manually and it is not good. I am not an [expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances) in MVC so apreciate any help. [Controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc):

UsersContext db = new UsersContext();

[public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [ActionResult](https://www.mindstick.com/forum/33961/what-is-the-difference-between-actionresult-and-viewresult) Index()

{

SelectList list = new SelectList(Roles.GetAllRoles());

ViewBag.Roles = list;

List<SelectListItem> items = new List<SelectListItem>();

[foreach](https://www.mindstick.com/forum/33870/how-parallel-foreach-works-internally) (var user in db.UserProfiles.ToList())

{

SelectListItem li = new SelectListItem

{

Value = user.UserName,

Text = user.UserName

};

items.Add(li);

}

ViewBag.UserName = items;

return View(db.UserProfiles.ToList());

}

[HttpPost]

[[ValidateAntiForgeryToken](https://www.mindstick.com/forum/34068/what-is-use-of-validateantiforgerytoken-in-asp-dot-net-mvc-views)]

public ActionResult Index([string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) RoleName, string UserName)

{

SelectList list = new SelectList(Roles.GetAllRoles());

ViewBag.Roles = list;

return View();

}

Model is of SimpleMembershipprovider, that is created with project.

View:

@model IEnumerable<UserProfile>

@Html.DropDownList("UserName", ViewBag.UserName as SelectList) - here [mistake](https://yourviews.mindstick.com/view/88393/ai-humanity-s-greatest-invention-or-final-mistake)

What should i do to [resolve](https://www.mindstick.com/forum/159427/windows-app-dll-not-found-resolve-library-issue)?

## Replies

### Reply by Barbara Jones

Do like this:

ViewBag.cityid = new SelectList(db.hr_pr_cities, "CityID", "Name");

and in view like this:

@Html.DropDownList("cityid", null, "Select City", null)

### Reply by Anonymous User

Try this:

@model IEnumerable<UserProfile>

@Html.DropDownList("UserName",new SelectList(ViewBag.UserName, "Value", "Text"))

Or change the type of your UserName to SelectList and then modify it here:

ViewBag.UserName = new SelectList(items, "Value", "Text");

//instead of ViewBag.UserName = items;

### Reply by Anonymous User

Problem is with the below code -

public ActionResult Index(string RoleName, string UserName)

{

SelectList list = new SelectList(Roles.GetAllRoles());

ViewBag.Roles = list;

return View();

}

In above code you never initiated - ViewBag.UserName, but your view is expecting UserName property in ViewBag (you only initiated Roles). you initiate it and problem will be done.


---

Original Source: https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
