Home > DeveloperSection > Forums > There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'UserName'
Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    December-19-2014 10:35 PM

 ASP.Net C# 
Ratings:


 3 Reply(s)
 7093  View(s)
Rate this:

There is no ViewData 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 in MVC so apreciate any help. Controller:

 

    UsersContext db = new UsersContext();

 

    public ActionResult Index()

    {

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

        ViewBag.Roles = list;

 

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

        foreach (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]

    public ActionResult Index(string 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

What should i do to resolve?



Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    December-20-2014 5:47 AM

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.


Pawan Shukla
Pawan Shukla

Total Post:29

Points:203
Posted on    December-20-2014 6:30 AM

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;


Barbara Jones

Total Post:13

Points:91
Posted on    December-20-2014 6:32 AM

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)


Don't want to miss updates? Please click the below button!

Follow MindStick