title: Setting an existing value for dropdown in MVC C# with Linq
body: <p>I have a dropdown list rendered from a database, just need to be able to set an existing value (if it exists). _jobService.GetTenancyForJob gets a value if one already exists and the other service just returns an id and value for each item. I need to be able to set the selected list item here in the controller but just struggling with the syntax (yeah it's using viewbag just for testing will be using ViewModel).</p>
  
  <p>I've done this before in a wide variety of ways just wondering how it would be done in this scenario... Any pointers appreciated.</p>
  
  <pre><code>var jobTenancies = _jobService.GetTenancyForJob(id);
  var getTenancies = _jobService.GetAllJobTenancies();
  
  var tenancyList = getTenancies.Select( t =&gt; new SelectListItem()
  {
    Text = t.JobTenancyName, 
    Value = t.Id.ToString()
  }).ToList();
  tenancyList.Insert(0, new SelectListItem() { Text="", Value = "" } );
  Viewbag.TenancyList = tenancyList;
  </code></pre>
  
  <p>Edit: in the view</p>
  
  <pre><code>&lt;div class="control-group"&gt;
    @Html.BootstrapLabelFor(model =&gt; model.TenancyName)
    &lt;div class="controls"&gt;
      @Html.DropDownListFor(x=&gt;x.TenancyId, (List&lt;SelectListItem&gt;)ViewBag.TenancyList)
      @Html.BootstrapValidationMessageFor(model =&gt; model.TenancyId)
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>