1

CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments

I am getting the user roles form the memebership provider and then asking the user to select the role. the roles are in a dropdown list on the register page.

public ActionResult ChangePassword() 
        {

            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            var roles = Roles.GetAllRoles().ToList();       
            ViewData["Roles"] = new SelectList(roles);

            return View();
        }

<p>
               <%= Html.DropDownList("Userrole",((SelectList)ViewData["Roles"]).Items,"--Select One---") %>
                </p>

2 Answers 2

1

Try

  <%= Html.DropDownList("Userrole",((SelectList)ViewData["Roles"]),"--Select One---") %>

update

There is something else that is causing the issue(do you have other extensions for the DropDownlist?)

The following works for me:

Action

 public ActionResult About()
        {
            var x = new List<string>
            {
                "A", "B", "C"
            };
            var y = new SelectList(x);
            ViewData["z"] = y;
            return View();
        }

View

 <%= Html.DropDownList("Userrole",((SelectList)ViewData["z"]),"--Select One---") %>
Sign up to request clarification or add additional context in comments.

3 Comments

It's giving me this error when i dont have that Exception Details: System.InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Userrole'.
I copy pasted and ran the same code that you sent me , it still throws the above error.
Sorry ,I got it , I made the changes as your code and it looks good now. Thank You so much Raj , appreciate your help.
0

controller code

  public ActionResult About()
    {
        var x = new []
        {
            "A", "B", "C"
        };
        var y = new SelectList(x);
        ViewData["z"] = y;
        return View();
    } 

it is your view code you try this it is working fine

<%: Html.DropDownList("Userrole", ViewData["z"] as SelectList,"--selectone--")%>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.