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 8452423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:29:46+00:00 2026-06-10T11:29:46+00:00

I’m working on an mvc .net web application and I’m using Entity Framework. In

  • 0

I’m working on an mvc .net web application and I’m using Entity Framework. In my model, I have an entity called “utisateur” (user) and every user has one or more users that supervise him. What I want to do is generate a multi select list box that contains the list of all users inorder to select the new users’ supervisors.

I tried to to that but after filling my form for creating a new user and submitting it i got this error : Object reference not set to an instance of an object, however I got the select list and I selected items from it. (the empty object is “model.selectedusers” but I don’t know why is it empty!)

Here is my model class :

public class util 
{
    public util()
    {
        user = new utilisateur();
        listesups = Getutilisateurs(null);
    }
    public utilisateur user { get; set; }
    public int[] selectedusers;
    public string nom_role { get; set; }
    public MultiSelectList listesups { get; set; } 

    public MultiSelectList Getutilisateurs(int[] selectedValues)
    {
        var db = new BDGestionEntities();
        List<utilisateur> utilisateurs = db.utilisateurs.ToList();
        return new MultiSelectList(utilisateurs, "id", "login", selectedValues);
    }
}

Here is my controller :

public ActionResult Create2()
    {
        util u = new util();
        u.listesups = new MultiSelectList (db.utilisateurs, "id", "login");
        return View(u);
    }
    [HttpPost]
    public ActionResult Create2(util model)
    {
        utilisateur u = new utilisateur();
        try
        {
            foreach (var selecteduse in model.selectedusers)
            {
                utilisateur utilisateur = db.utilisateurs.Where(c => c.id == selecteduse).FirstOrDefault();
                u.superieur.Add(utilisateur);
            }
        }
        catch(Exception ex)
        {
            Logger.Error(ex.Message, "Erreur ajout supérieurs");
        }
        u.nom = model.user.nom;
        u.prenom = model.user.prenom;
        u.solde_conge = model.user.solde_conge;
        u.email = model.user.email;
        u.login = model.user.login;
        u.pwd = model.user.pwd;
        role role = new role();
        role = db.roles.Where(c => c.nom_role == model.nom_role).FirstOrDefault();
        u.role = role;
        db.utilisateurs.AddObject(u);
        db.SaveChanges();
        ViewBag.id_role = new SelectList(db.roles, "id", "nom_role", model.user.id_role);
        return RedirectToAction("index");

    }

Here is the part of the view that contains the selectlist

@Html.ListBoxFor(model => model.selectedusers, Model.listesups)

And here what I got before submitting the form, as you can see here the view contains the seleclist but selected items are not saved

enter image description here

  • 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-10T11:29:48+00:00Added an answer on June 10, 2026 at 11:29 am

    There is no built in support for including multiple items (or an arry or items) into the viewModel from View by default in MVC 3. That is why you are losing the selected value.

    There are many ways of making it happen but the easier one is to modify your method’s signature to include form collection and query the values of the listbox.
    Your Create2 method should look like:

        [HttpPost]
            public ActionResult Create2(FormCollection collection,util model)
            {
               //load selected users into a variable
               var selUsers= collection["selectedusers"];
    
              //do whatever you want to do with the selected users. 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.