{"id":2887,"date":"2014-03-10T02:38:38","date_gmt":"2014-03-10T02:38:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/10\/multi-select-list-box-asp-mvc-collection-of-common-programming-errors\/"},"modified":"2014-03-10T02:38:38","modified_gmt":"2014-03-10T02:38:38","slug":"multi-select-list-box-asp-mvc-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/10\/multi-select-list-box-asp-mvc-collection-of-common-programming-errors\/","title":{"rendered":"Multi select list box asp mvc-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m working on an mvc .net web application and I&#8217;m using Entity Framework. In my model, I have an entity called &#8220;utisateur&#8221; (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&#8217; supervisors.<\/p>\n<p>I tried to to that but i got this error : Object reference not set to an instance of an object.<\/p>\n<p>Here is my model class :<\/p>\n<pre><code>public class util \n{\n    public util()\n    {\n        user = new utilisateur();\n        listesups = Getutilisateurs(null);\n    }\n    public utilisateur user { get; set; }\n    public int[] selectedusers;\n    public MultiSelectList listesups { get; set; }       \n    public MultiSelectList Getutilisateurs(int[] selectedValues)\n    {\n        var db = new BDGestionEntities();\n        List utilisateurs = db.utilisateurs.ToList();\n        return new MultiSelectList(utilisateurs, \"id\", \"login\", selectedValues);\n    }\n}\n<\/code><\/pre>\n<p>And here is the part of the view that contains the list box :<\/p>\n<pre><code> @Html.ListBoxFor(model =&gt; model.selectedusers, Model.listesups)\n<\/code><\/pre>\n<p>Here is the controller<\/p>\n<pre><code>    public ActionResult Create2()\n    {\n        return View();\n    }\n\n[HttpPost]\n    public ActionResult Create2(util model)\n    {\n        utilisateur u = new utilisateur();\n        if (model.selectedusers != null)\n        {\n            foreach (var selecteduse in model.selectedusers)\n            {\n                int selecteduseId = selecteduse;\n                utilisateur utilisateur = db.utilisateurs.Where(c =&gt; c.id == selecteduseId).FirstOrDefault();\n                u.superieur.Add(utilisateur);\n            }\n\n\n        }\n        u.nom = model.user.nom;\n        u.prenom = model.user.prenom;\n        u.solde_conge = model.user.solde_conge;\n        u.email = model.user.email;\n        u.login = model.user.login;\n        u.pwd = model.user.pwd;\n        role role = new role();\n        \/\/role.nom_role = model.nom_role;\n\n        role = db.roles.Where(c =&gt; c.nom_role == model.nom_role).FirstOrDefault();\n        u.role = role;\n        db.utilisateurs.AddObject(u);\n        db.SaveChanges();\n        ViewBag.id_role = new SelectList(db.roles, \"id\", \"nom_role\", model.user.id_role);\n        return RedirectToAction(\"index\");\n\n    }\n<\/code><\/pre>\n<p>And here is the error message<\/p>\n<pre><code>Server Error in '\/' Application.\nObject reference not set to an instance of an object.\nDescription: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. \nException Details: System.NullReferenceException: Object reference not set to an instance of an object.\n\nSource Error: \nLine 44:         <\/code><\/pre>\n<tr>\n<td><code><b>Role :<\/b><\/code><\/td>\n<td>@Html.DropDownListFor(model =&gt; model.nom_role, values)<\/td>\n<\/tr>\n<p>Line 45:<\/p>\n<tr>\n<td><b>Liste des sup\u00e9rieurs :<\/b><\/td>\n<td>@Html.ListBoxFor(model =&gt; model.selectedusers, Model.listesups)<\/td>\n<\/tr>\n<p>Line 47: Line 48:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working on an mvc .net web application and I&#8217;m using Entity Framework. In my model, I have an entity called &#8220;utisateur&#8221; (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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2887","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2887"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2887\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}