DropDownList not workinghttp://forums.asp.net/t/1800666.aspx/1?DropDownList+not+workingMon, 07 May 2012 12:15:50 -040018006664967782http://forums.asp.net/p/1800666/4967782.aspx/1?DropDownList+not+workingDropDownList not working <p>Hello!</p> <p>Im trying to populate a dropdownlist with a list of value that sometimes may come in with a selected value. My problem is that it gives me this error:</p> <p>The ViewData item that has the key 'ModuleID' is of type 'System.Guid' but must be of type 'IEnumerable&lt;SelectListItem&gt;'.</p> <p>This is my view:</p> <p>&lt;div class=&quot;editor-label&quot;&gt;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.LabelFor(model =&gt; model.ModuleID, &quot;Module&quot;)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/div&gt;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;div class=&quot;editor-field&quot;&gt;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Html.DropDownList(&quot;ModuleID&quot;, &quot;Please select&quot;)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Html.ValidationMessageFor(model =&gt; model.ModuleID)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/div&gt;</p> <p>&nbsp;</p> <p>This is my Controller with the Action and method to populate the list:</p> <p>void PopulateModuleList(Guid selectedModule)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List&lt;Module&gt; modules = SecurityModuleViewModelBuilder.ConvertToModules(repository.GetModules(true));</p> <p>&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Module module = modules.Find(m =&gt; m.ID == selectedModule);</p> <p>&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewBag.ModuleID = new SelectList(modules, &quot;ID&quot;, &quot;Name&quot;, module);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public ActionResult CreateTask(Guid? id)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Task task = new Task();</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (id.HasValue &amp;&amp; id.Value != Guid.Empty)</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PopulateModuleList(id.Value);</p> <p>&nbsp;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return View(task);</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>The behaviour Im looking for is that when an GUID is past that value comes in as selected on the view and when it is not, display Please select.</p> <p>Thanks!</p> 2012-05-06T16:53:01-04:004967797http://forums.asp.net/p/1800666/4967797.aspx/1?Re+DropDownList+not+workingRe: DropDownList not working <p></p> <blockquote><span class="icon-blockquote"></span> <h4>humble-apprentice</h4> The ViewData item that has the key 'ModuleID' is of type 'System.Guid' but must be of type 'IEnumerable&lt;SelectListItem&gt;'.</blockquote> <p></p> <p>Where do you populate with System.Guid ? please show all code!</p> 2012-05-06T17:15:22-04:004969045http://forums.asp.net/p/1800666/4969045.aspx/1?Re+DropDownList+not+workingRe: DropDownList not working <p>ModuleID is a property of Task of type Guid.</p> <p>This is how I solved the issue:</p> <pre class="prettyprint">void PopulateModuleList(Guid selectedModule) { List&lt;Module&gt; modules = SecurityModuleViewModelBuilder.ConvertToModules(repository.GetModules(true)); if (selectedModule != Guid.Empty) ViewBag.ModuleID = new SelectList(modules, &quot;ID&quot;, &quot;Name&quot;, selectedModule); else ViewBag.ModuleID = new SelectList(modules, &quot;ID&quot;, &quot;Name&quot;); } public ActionResult CreateTask(Guid? id) { Task task = new Task(); if (id.HasValue &amp;&amp; id.Value != Guid.Empty) PopulateModuleList(id.Value); else PopulateModuleList(Guid.Empty); return View(task); }</pre> <p>This code populates the dropdown, if an ID is passed it is set as selected and if not it just displays the list.</p> 2012-05-07T12:15:50-04:00