Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:40:24+00:00 2026-06-01T16:40:24+00:00

Been at this for too long! any advice on the following problem… code added:

  • 0

Been at this for too long! any advice on the following problem…

code added:

Model: AccountModels.cs

[Required]
[Display(Name = "Select role: ")]
public String Role { get; set; }

Controller: AccountController.cs

  // GET: /Account/Register

public ActionResult Register()
{ 
    List<SelectListItem> list = new List<SelectListItem>();
    SelectListItem item; 
    foreach (String role in Roles.GetAllRoles()) 
    { 
        item = new SelectListItem { Text = role, Value = role }; 
        list.Add(item); 
    }
    ViewBag.roleList = (IEnumerable<SelectListItem>)list;
    return View();
}

Also in the [HTTpPost]

if (createStatus == MembershipCreateStatus.Success)
{
    Roles.AddUserToRole(model.UserName, model.Role);

    MailClient.SendWelcome(model.Email);
    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
    return RedirectToAction("Create", "Customers");
}

View: Register.cshtml

<div class="editor-label">      
    @Html.LabelFor(m => m.Role)  
</div>      
<div class="editor-field">
    @Html.DropDownListFor(m => m.Role, ViewBag.roleList as IEnumerable<SelectListItem>)

The code works and allows the user to select a role but falls apart if the user breaks validation rules when creating a new account.

Example: if the passwords don’t match normally validation would kick in but due to the addition of the new code the application just crashes.

The code which creates the error:

@Html.DropDownListFor(m => m.Role, ViewBag.roleList as IEnumerable<SelectListItem>)

Error code

The ViewData item that has the key ‘Role’ is of type ‘System.String’ but must be of type ‘IEnumerable’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:40:26+00:00Added an answer on June 1, 2026 at 4:40 pm

    I guess you have this problem occuring when in your HttpPost action method where your Validation fails. and i believe you have return View() statement in your HttpPost action method. When you return the View again, your ViewData is null. So you need to reload the data again for the dropdown

    public ActionResult Register()
    {
     //If validation ok, save
     if (createStatus == MembershipCreateStatus.Success)
     {
       //dio stuff
     }
     else
     { 
        List<SelectListItem> list = new List<SelectListItem>();
        SelectListItem item; 
        foreach (String role in Roles.GetAllRoles()) 
        { 
            item = new SelectListItem { Text = role, Value = role }; 
            list.Add(item); 
        }
        ViewBag.roleList = (IEnumerable<SelectListItem>)list; 
        return View();
     }
    }
    

    Ideally, I would not use the ViewData to hande this. I will have my ViewModel to handle this.

    public class RegisterViewModel
    {
     public string FirstName { set;get;} 
     public IEnumerable<SelectListItem> Roles { get; set; }
     public int SelectedRoleId { get; set; }
    }
    

    and in your get action call,pass the ViewModel back to your View.

    public ActionResult Register()
    {
      RegisterViewModel objViewModel=new RegisterViewModel();
      objViewModel.Roles=GetAllRolesAs();  // returns the list of roles to this property
      return View(objViewModel);
    }
    

    Make your View a strongly typed one

    @model RegisterViewModel
    @using (Html.BeginForm())
    {
       @Html.TextBoxFor(m => m.FirstName)
    
       @Html.DropDownListFor(x => x.SelectedRoleId,new SelectList(Model.Roles , "Value","Text"),   "Select Role")
      <input type="submit" value="Save" />
    }
    

    and in your HttpPostAction, Receive this ViewModel object as the parameter

    [HttpPost]
    public ActionResult Register(RegisterViewModel objVM)
    {
      if(ModelState.IsValid)
      {
        //Check your custom validation also
        if (createStatus == MembershipCreateStatus.Success)
        {
              //Save and then Redirect
    
        }
        else
        {
          objVM.Roles=GetAllRolesAs(); // get the list of roles here again because HTTP is stateless
        }
      }  
     return View(objVM);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been looking at this code for too long and I am getting gloomy any
I've been looking for solutions to this problem for far too long considering how
I've been thinking about this far too long and haven't gotten any idea, maybe
I've been scratching my head at this for too long - any insight would
I've been struggling with this for far too long, and I don't see any
I've been working on this problem way too long, and I'm getting nowhere. So
Been at this far too long; trying to populate a field in a record
I think I've just been looking at this too long. I have some data
It might be that I've been staring at this for too long, and the
Been playing with this for too long now! I'm using codeigniter. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.