title: set select value in IEnumerable<SelectListItem> c# mvc
body: <p>I have a <code>IEnumerable&lt;SelectListItem&gt;</code></p>
  
  <p>My ViewModel</p>
  
  <pre><code>public class StatusClass
  {
      public string Status { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; StatusList { get; set; }
  }
  </code></pre>
  
  <p>I set values in to <code>StatusList</code> from my controller.</p>
  
  <pre><code>StatusClass statusObj = new CRM.StatusClass();
  
  List&lt;SelectListItem&gt; Discountdata = new List&lt;SelectListItem&gt;();
          Discountdata.Add(new SelectListItem() { Value = "All", Text = "All" });
          Discountdata.Add(new SelectListItem() { Value = "Draft", Text = "Draft" });
          Discountdata.Add(new SelectListItem() { Value = "Issued", Text = "Issued" });
          Discountdata.Add(new SelectListItem() { Value = "Partial", Text = "Partially Received" });
          Discountdata.Add(new SelectListItem() { Value = "Received", Text = "Received" });
          Discountdata.Add(new SelectListItem() { Value = "PAID", Text = "Paid" });
          Discountdata.Add(new SelectListItem() { Value = "Billed", Text = "Billed" });
  
          statusObj.StatusList = new SelectList(Discountdata, "Value", "Text"); 
  </code></pre>
  
  <p>This works fine and my HTML is like this:</p>
  
  <pre><code>@Html.DropDownListFor(model =&gt; model.Status, Model.StatusList)
  </code></pre>
  
  <blockquote>
    <p>What i want is, I need to set the selected value from the controller when the list get created.</p>
  </blockquote>
  
  <p>Suppose I have a string like this:</p>
  
  <pre><code>string newwStatus = "Issued";
  </code></pre>
  
  <p>How can I set it as selected in the <code>SelectListItem</code>.</p>
  
  <p>I tried this, but its not working in my case:</p>
  
  <pre><code>foreach(var item  in StatusList)
  {
      if(item.value == status)
      {
          item.Selected = true;
      }
  }
  </code></pre>
  
  <p>and tries this too:</p>
  
  <pre><code>IEnumerable&lt;SelectListItem&gt; selectList =
      from s in StatusList
          select new SelectListItem
          {
              Selected = (s.Value == status),
              Text = s.Text,
              Value = s.Value
          };
  </code></pre>
  
  <p>I dont know if these are the right way if someone know how to do this, please help.</p>
  
  <p>Thanks in advance.</p>