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

  • SEARCH
  • Home
  • 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 7569739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:14:30+00:00 2026-05-30T15:14:30+00:00

In my application I have associated my UserId to a table in my database.

  • 0

In my application I have associated my UserId to a table in my database. I need that when I create a new item I can choose the user name from a dropdownlist. And ‘possible to do this with the element viewbag?

@Html.EditorFor(model => model.UserId)

I use default membership provider so I can’t use Entity Framework for this problem

EDIT

EDIT 2

This is my action create:

[HttpPost]
public ActionResult Create(Employe employe)
{
        var users = Roles.GetUsersInRole("Admin");
        SelectList list = new SelectList(users);
        ViewBag.Users = list;
        if (ModelState.IsValid)
        {
            **employe.EmployeID = users;**
            db.Employes.Add(employe);
            db.SaveChanges();
}

This does not work. The error is:

Cannot implicitly convert type ‘string[]’ to ‘string’

My model for Employee

public class Employee
{
        [Key]
        public int EmployeID { get; set; }

        public Guid UserId { get; set; }

        public string Name { get; set; }

        [ForeignKey("UserId")]
        public virtual MembershipUser User
        {
            get
            {
                return Membership.GetUser(this.Name); //Changed this to Name 
            }
        }

    }
}

View:

@Html.DropDownList("Users", ViewBag.Users as SelectList);

My result in UserId field isn’t a UserId but this 000000-000000-0000000-00000

  • 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-05-30T15:14:31+00:00Added an answer on May 30, 2026 at 3:14 pm

    How to set a list of users as a SelectItem in the ViewBack
    Yes, you should be able to do this by passing your collection to the ViewBag and then create you dropdown from it:

    In your controller

            var users = Roles.GetUsersInRole("Admin");
            SelectList list = new SelectList(users);
            ViewBag.Users = list;
    

    In your View (If you’re using Razor)

    @Html.DropDownList("Users", ViewBag.Users as SelectList);
    

    Read more about SelectListItem here:

    • http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.aspx

    Also check out:

    • How can I get this ASP.NET MVC SelectList to work?
    • Problem with ASP.Net MVC SelectLIst and List<SelectListItems>

    Question changed to something more. Here is my idea to solve the issue:

    Controller:

    public ActionResult Mirko() {
        List<SelectListItem> items = new List<SelectListItem>();    
        foreach (string userName in Roles.GetUsersInRole("Admin")) {
            var user = Membership.GetUser(userName);
            SelectListItem li = new SelectListItem {
                Value = user.ProviderUserKey.ToString(),
                Text = user.UserName,
            };
            items.Add(li);
        }
        items.Add(new SelectListItem { Text = "Please Select...", Value = "na" , Selected = true});
        ViewBag.Users = items;
        return View();                
    }
    
    [HttpPost]
    public ActionResult Mirko(Employee employee) {
        if(IsValideEmployee(employee)) {
            /*Only used to show that user was retrieved*/
            TempData["message"] = "Saved Employee";
            TempData["user"] = employee.User;
    
           /* employeeRepository.Save(employee) */
    
            /* Redirect to where you want to go */
            return RedirectToAction("Mirko", "Home");
        }
        return View(employee);
    }
    
    private bool IsValideEmployee(Employee emp) {
        if (emp.Name == "na")
            ModelState.AddModelError("UserId", "You must select a user!");
        /*Do some validation here*/
        //ModelState.Add("Name", "You must set the user name!")
        return ModelState.IsValid;
    }
    

    View

    @model StackOverFlowExample.Models.Employee
    @{
        MembershipUser user = null;
        ViewBag.Title = "Mirko Example";
        var users = ViewBag.Users as IEnumerable<SelectListItem>;
    }
    
    @if (TempData["message"] != null) {
        user = TempData["user"] as MembershipUser;
        <h3>@TempData["message"]</h3>
        <div>
            <span>You selected @user.UserName</span>
            <ul>
                <li>Email: @user.Email</li>
                <li>Last Logged In: @user.LastLoginDate.ToString("d")</li>
                <li>Online: @user.IsOnline</li>
            </ul>
        </div>
    }
    
    @using (@Html.BeginForm()) { 
        <label for="UserId">Associate Employee To User:</label>
        @Html.DropDownListFor(m => m.UserId, @users)
        @Html.HiddenFor(m => m.Name)                                               
        <input type="submit" value="Save" id="save-employee"/>
    }
    <div id="status" style="display:none;"></div>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $("#UserId").change(function () {
                //Set value of name 
                $("#Name").val($(this).children("option:selected").text());            
            });
            $("#save-employee").click(function (e) {
                var value = $("#Name").val();
                if (value == "" || value == "na") {
                    e.preventDefault();
                    $("#status").html("<h3>You must select a user!</h3>").toggle();
                }
            });
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have a sqlite database that looks like this: CREATE TABLE
I'm looking to have windows recognize that certain folders are associated to my application
I have associated my app with a UTI so that users can launch KML
We have an application that needs to access a database that is owned by
So I have an application that has a special file type .rxml associated to
I have a click-once application. I have an associated file that I store the
I have an application which has multiple activities associated with it. When the user
I have application that makes different queries with different results so the caching in
I have application that is connecting to the DB and if I enter incorrect
I have an application that I know would make a great cube and would

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.