0

I am merely trying to get items into an Html.DropDownList() as follows. I've debugged and know the data is coming across to the View but the dropdownlist is not displayed. Anyone know where I'm going wrong here?

@model List<ControlNumberViewer.Models.Table>

@{ Layout = null; }

Control Number Viewer

 <div id="PickTable">
@{Html.DropDownList("TableName", Model.Select(x => new SelectListItem
  {
      Text = x.TableName,
      Value = x.TableName
  }));
 }


</div>
Matt
  • 5,542
  • 14
  • 48
  • 59
  • Tried the DropDownListFor(...)? :) http://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor – Nicklas Pouey-Winger May 15 '14 at 16:39
  • Well DropDownlistFor works cool which is simple to understand . `@Html.DropDownListFor(m => m.tablename,Model=>Model.Yourlist_Model_Here)` . Additionally you can have id etc – super cool May 15 '14 at 16:45
  • Yes I've tried it though I must admit I don't know the difference between the two types of dropdowns.. – Matt May 15 '14 at 16:49

2 Answers2

0

Pull your data into the controller and store it in a ViewBag. Then use DropDownListFor for strongly typed access to the data for the property TableName.

in your controller:

var linqQuery = _db.SQLTable.Select(x => new { Text = x.TableName, Value = x.TableName });
ViewBag.ListValues = new SelectList(linqQuery, "Value", "Text");

in your view:

@Html.DropDownListFor(m => m.TableName, ViewBag.ListValues as SelectList);
-1
  @{
      var listitem = Model.Select(x => new SelectListItem {
      Text = x.TableName,
      Value = x.TableName
  });
        Html.DropDownList("TableName", (SelectList)listitem);
    }
chandresh patel
  • 309
  • 1
  • 10
  • I've implemented this way and this is how it bombs:Unable to cast object of type 'WhereSelectListIterator`2[ControlNumberViewer.Models.Table,System.Web.Mvc.SelectListItem]' to type 'System.Web.Mvc.SelectList'. – Matt May 15 '14 at 16:31