title: MVC Edit View set selected dropdown list value with htmlAttribute overload
body: <p>So I can easily set the select value of my drop-down list in my Edit View if I <strong>don't</strong> using the htmlAttributes overloaded method. So the below <strong>works</strong>:</p>
  
  <p><strong>Edit View razor</strong></p>
  
  <pre><code> @Html.DropDownList("CategoriesId")
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    public ActionResult Edit(int? id)
      {
          if (id==null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          Budget budget = db.BudgetData.Find(id);
          if (budget == null)
          {
              return HttpNotFound();
          }
          ViewBag.CategoriesId = new SelectList(db.Categories, "Id", "Description", budget.CategoriesId);
  
  
          return View(budget);
      }
  </code></pre>
  
  <p>No problem with the above but I want to be able to set the class using the <em>htmlAttributes</em> without using jQuery or some similar way. </p>
  
  <p>As soon as I change my Edit View to the below it now has a nice bootstrap class but the selected value for that record has now been lost and it it always just shows the first record. I'm sure its something simple but I'm new to MVC :)</p>
  
  <p><strong>New htmlAttribute Razor</strong></p>
  
  <pre><code> @Html.DropDownList("CategoriesId", (IEnumerable&lt;SelectListItem&gt;)ViewBag.CategoriesId, new { @class="form-control"})
  </code></pre>
  
  <p>I have also tried the below controller code but to no avail.</p>
  
  <pre><code> ViewBag.CategoriesId = db.Categories.AsEnumerable().Select(s =&gt; new SelectListItem { Text = s.Description, Value = s.Id.ToString(), Selected = budget.CategoriesId == s.Id });
  </code></pre>
  
  <p>Thanx for the helps guys.</p>
  
  <p><strong>--EDIT FOR ANSWER INFO--</strong></p>
  
  <p>After finding out that my DDL with <em>ViewBag.CategoriesId</em> can't be the same as my model etc causing the above the question. I changed my ViewBag property to <em>ViewBag.CategoriesDDL</em> but then of course that's not in my model. </p>
  
  <p>So for any non mvc pro's like myself here's what I did to fix that. I only changed the following in my Edit View:</p>
  
  <pre><code>@Html.DropDownList("CategoriesDDL", (IEnumerable&lt;SelectListItem&gt;)ViewBag.CategoriesId, new { @class = "form-control" })
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>  @Html.DropDownListFor(model =&gt; model.CategoriesId, ViewBag.CategoriesDDL as IEnumerable&lt;SelectListItem&gt;, new { @class = "form-control" })
  </code></pre>
  
  <p>Hopefully this question helps somebody out there :)</p>