Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
    ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
    return View();
}


Register.cshtml
-----------------
HTML
<!--Select the Role Type for the User-->
    <div class="form-group">
        @Html.Label("Select Your User Type", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @*@Html.DropDownList("Name")*@
    @*@Html.DropDownList("Name", (SelectList)ViewBag.Name, "--Choose Role--")*@
            @Html.DropDownList("Name", (SelectList)ViewBag.Name, "--Choose Role--")
            
        </div>
    </div>
    <!--Ends Here-->


What I have tried:

The ViewData item that has the key 'Name' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
Posted
Updated 7-Apr-17 19:44pm
v2
Comments
Karthik_Mahalingam 8-Apr-17 1:27am    
have tested your code, it works fine.
you might be missing something

1 solution

refer this and fix it in your code

Controller
public class Role
   {
       public string Name { get; set; }
   }


   public class HomeController : Controller
   {
       public ActionResult Index()
       {
           List<Role> lst = new List<Role>();
           lst.Add(new Role() { Name = "Admin" });
           lst.Add(new Role() { Name = "Master" });
           lst.Add(new Role() { Name = "user" });
           ViewBag.Name = new SelectList(lst, "Name", "Name");
           return View();
       }

   }

View

@Html.DropDownList("Name", (SelectList)ViewBag.Name, "--Choose Role--")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900