title: Building a DropDownList in the Model?
body: <p>I have a service layer that exposes a method, which returns me a List, called GetStates.</p>
  
  <pre><code>public List&lt;StateObject&gt; GetStates()
  </code></pre>
  
  <p>My State Object is just a custom object I have created:</p>
  
  <pre><code>public class StateObject
  {
      public int stateId { get; set; }
      public string description { get; set; }
      public Boolean isDefault { get; set; }
  }
  </code></pre>
  
  <p>In my models, I am trying to create a model that will be used for my display and modification screen of a task. One thing this will be used for is handling the display and selection of a Drop down box, which will give a list of States available for my Task. So, my model looks something like this (Removed properties we don't car about - it's a bit bigger than this:</p>
  
  <pre><code>public class TaskModifyModel
  {
      public int stateId { get; set; }
      public string state { get; set; }
  
      public SelectList states { get; private set; }
  
      public TaskModifyModel()
      {
  
          states = new SelectList(
              (new ReferenceService().GetStates()),
              "stateId",
              "description",
              1);
  
      }
  }
  </code></pre>
  
  <p>So, stateId holds the selected state, state holds the text description of the selected state. In the constructor, I am attempting to create a states SelectList for the view... and populate it.</p>
  
  <p>In the view, I then try to display the Drop Down List:</p>
  
  <pre><code>@Html.DropDownListFor(m=&gt;m.stateId, new SelectList(Model.states, "stateId", "description", Model.priorityId))
  </code></pre>
  
  <p>This is failing, dismally.</p>
  
  <p><strong>DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'stateId'.</strong></p>
  
  <p>I have searched, and I thought I was doing this the right way, but the error says I am not.. clearly. :) Could someone guide me on why it's not working, and also, is this the right way to do things?</p>
  
  <p>Edit:
  After assistance below, it's now working. If I am creating a new task (taskId==0), then I have to get the default value of the dropdown, as stored in my database.... So, is this clean? This is my working constructor for the object:</p>
  
  <p>public TaskModifyModel()
      {
          var referenceService = new ReferenceService();
          var p = referenceService.GetPriorities();
          var s = referenceService.GetStates();</p>
  
  <pre><code>    var defaultP = (from a in p where a.isDefault select a).FirstOrDefault();
      var defaultS = (from a in s where a.isDefault select a).FirstOrDefault();
  
      priorities = new SelectList(
          (p),
          "priorityId",
          "description"
          );
      priorityId = taskId == 0 ? defaultP.priorityId : priorityId;
  
      states = new SelectList(
          s,
          "stateId",
          "description");
      stateId = taskId == 0 ? defaultS.stateId : stateId;
  
  }
  </code></pre>
  
  <p>Is it OK?</p>