tags:

views:

62

answers:

1

I'm adding the Roles provider to the built in AccountModel but having some problems adding GetAllRoles in my view using the Register View Model.

View Model from AccountModel

public class RegisterModel
    {
        UserName, Email Etc....

        [Required]
        [DisplayName("AllRoles")]
        public SelectList AllRoles { get; set; }
    }

Roles Service added to AccountModel

public interface IRolesService
{
    SelectList GetAllRoles();
}

    public class RolesService : IRolesService
{
    public SelectList GetAllRoles()
    {
        var AllRoles = new SelectList(Roles.GetAllRoles());
        return AllRoles;
    } 
}

Register View Page Inherits RegisterModel

           Form...

            <div class="editor-label">
                <%= Html.LabelFor(m => m.ConfirmPassword) %>
            </div>
            <div class="editor-field">
                <%= Html.PasswordFor(m => m.ConfirmPassword) %>
                <%= Html.ValidationMessageFor(m => m.ConfirmPassword) %>
            </div>

            <%= Html.DropDownListFor(m => m.AllRoles)%>

I'm not sure how to populate the DropDown list with all the Roles from the View Model.

Any help would be really great!!

A: 

I think you need properties for the selected role and the full list of roles. The list of roles will be used to populate the dropdown, the selected role will be populated on post with the selected value.

public class RegisterModel
{
    UserName, Email Etc....

    [Required]
    [DisplayName("Role")]
    public string Role { get; set; }

    [ScaffoldColumn(false)]
    public SelectList AllRoles { get; set; }
}

...
public ActionResult Register()
{
     var roleService = new RoleService();
     var model = new RegisterModel
     {
          AllRoles = roleService.GetAllRoles(),
          // Role = "User" if you want to choose a default
     }

     return View( model );
}


<div class="editor-label">
   <%= Html.LabelFor(m => m.ConfirmPassword) %>
</div>
<div class="editor-field">
   <%= Html.PasswordFor(m => m.ConfirmPassword) %>
   <%= Html.ValidationMessageFor(m => m.ConfirmPassword) %>
</div>

<%= Html.DropDownListFor(m => m.Role, Model.AllRoles, "--select--", null )%>
tvanfosson
That makes sense although I'm getting the following error in my view for the HTML.DropDownListFor code... Use the New keyword to create the instance
Jemes
@Jemes - Try using it without the `"--select--", null` and see if you still get the error.
tvanfosson
Did'nt work still got the same error.
Jemes
Are you sure that you're populating the model on the view? and that it's not null?
tvanfosson
I think so. I've noticed that there is a DropDown and a DropDownFor HTML Helper should I be using DropDown?
Jemes
I'm getting a blank DropDown List now so no errors but no data either?
Jemes
It sounds like the problem is with your model. Is your action populating the model as I've shown.
tvanfosson
Got it working just fixed a problem in my model, I was missing. Thanks for you help!!
Jemes