title: How to set selected item to a dynamic dropdown list in Edit Mode MVC 5
body: <pre><code>@if (ViewBag.language != null)
  {
      foreach (var item in ViewBag.language)
      {
          &lt;div class="control-label col-md-2"&gt;Language&lt;/div&gt;
          &lt;div class="col-md-6"&gt;
              @Html.DropDownList("LanguageId", null, new { @class = "form-control" },@item.LanguageID)
          &lt;/div&gt;
      }
  }
  </code></pre>
  
  <p>the <code>ViewBag.Langauge</code> contains the <code>LangaugeID</code> and <code>foreach</code> iteration I want to set the dropdownlist selected value as value of <code>@item.langaugeID</code></p>
  
  <pre><code>ViewBag.LanguageId = new SelectList(db.Languages, "LanguageId", "LanguageName");
  </code></pre>
  
  <p>Contains all the languages in database </p>
  
  <pre><code>for (int i = 0; i &lt; count; i++)
  {
      int languageid = StudentLanuguages[i];
      List&lt;tblStudentLanguageMapping&gt; StudentLan = new List&lt;tblStudentLanguageMapping&gt;();
      StudentLan = (from a in db.tblStudentLanguageMappings 
                    where a.StudentLanguageMappingId == languageid
                    select a).ToList();
      std.AddRange(StudentLan);
  }
  ViewBag.language = std;
  </code></pre>