2014-05-25 39 views
1

我不明白爲什麼我的編輯頁面無法在我的下拉列表中獲取選定的值。Asp.net MVC5 Bootstrap:無法在編輯頁面中獲取選定的值Dropdownlist

我的創建功能工作正常,但我的編輯頁面不在我的下拉列表中顯示選定的值。我試圖通過使用Viewbags獲取選定的值,如果我正在調試,ID的存在。

這是我Edit()

public ActionResult Edit(int? id) 
    { 
     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     SeminarErstellen seminarerstellen = db.SeminarErstellen.Find(id); 
     if (seminarerstellen == null) 
     { 
      return HttpNotFound(); 
     } 
     ViewBag.FileUploadId = new SelectList(db.FileUpload, "FileUploadId", "FilePath", seminarerstellen.FileUploadId); 
     ViewBag.LehrbeauftragterId = new SelectList(db.Lehrbeauftragter, "LehrbeauftragterId", "Vorname", seminarerstellen.LehrbeauftragterId); 
     ViewBag.SeminarStatiId = new SelectList(db.SeminarStati, "SeminarStatiId", "StatusTyp", seminarerstellen.SeminarStatiId); 
     ViewBag.SeminarTypId = new SelectList(db.SeminarTyp, "SeminarTypId", "Seminarbezeichnung", seminarerstellen.SeminarTypId); 
     return View(seminarerstellen); 
    } 

這是我查看下拉列表:問題是,它總是選擇我的下拉列表的第一個項目,而不是選擇一個...我希望你能理解。 ..

<div class="form-group"> 
     @Html.LabelFor(model => model.SeminarTypId, "SeminarTypId", new { @class = "control-label col-md-2" }) 
     <div class="col-md-3"> 
      @* This Dropdown is selecting the right value but without Bootstrap!*@ 
      @*@Html.DropDownList("SeminarTypId", String.Empty)*@ 
      @* This line is selecting the wrong item, its selecting the first item of the list*@ 
      @Html.DropDownList("SeminarTypId", (SelectList)ViewBag.SeminarTypId, new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.SeminarTypId) 
     </div> 
</div> 

回答

0

對於去除你的困惑

......................

    1. 自舉類什麼都沒有做與你的DropDownList
  • 2.問題與您的命名約定

解決方案

問題是您的ViewBag屬性名稱。因爲它與Model中的屬性相同,所以不起作用。


YourCode

Controller

ViewBag.SeminarTypId = new SelectList(db.SeminarTyp, "SeminarTypId", minarbezeichnung",seminarerstellen.SeminarTypId); 

View

@Html.DropDownList("SeminarTypId", (SelectList)ViewBag.SeminarTypId, new { @class = "form-control" }) 

Modification

Controller

ViewBag.SeminarTypId123 = new SelectList(db.SeminarTyp, "SeminarTypId", minarbezeichnung",seminarerstellen.SeminarTypId); 

View

@Html.DropDownList("SeminarTypId", (SelectList)ViewBag.SeminarTypId123, new { @class = "form-control" }) 
+0

嘿ShekharPankaj, – Armando82

+0

非常感謝你...是其工作的罰款... – Armando82

+1

然後標記問題的解決 –

相關問題