2016-06-09 32 views
0

Im有點卡住了,我有一個下拉,我試圖將兩個字段從同一個表中。它有點工作,這裏是我得到的: The offending drop down list新SelectList結合到字段,但只有一個

正如你所看到的,即時通訊拉東西,我不想進入它,並即時通訊不拉我的姓。

這裏是我的編輯器:

var DocList = 
      db.Doctors 
      .Select(s => new 
      { 
       Value = s.ID, 
       FirstN = s.FirstName, 
       SurN = s.Surname, 

      }) 
      ; 
ViewBag.Doctor = new SelectList(DocList, "FirstN", "SurN", episode.Doctor.ID); 

,這裏是我的剃鬚刀片段:

@Html.DropDownList("docTest") 

你們覺得這是怎麼回事錯在這裏?提前致謝! :)

+0

你是什麼意思通過此代碼'ViewBag.DoctorOLD = new SelectList(DocList,「FirstN」,「SurN」,episode.Doctor.ID);' –

+0

當在ViewBag中使用DoctorOLD'時,這是什麼'docTest' –

+0

Where is你綁定在剃鬚刀視圖中查看ViewBag.DoctorOLD。似乎你只是下拉列表的指定名稱。 – Oasis

回答

0

好這裏是解決方案IM還是蠻綠色在這個故凡我遭遇了挫折是知道過載。讓我試着解釋(並糾正我,如果我錯了)

好吧,所以我們援引我們的名單。現在我們的第一個重載是易編碼的,這就得到了我們希望列在我們列表中的一堆東西。接下來,我們需要我們的值字段,即表中其他表用於鏈接到它的表中的列,在大多數情況下,它是ID列。

接下來是字符串。在我的情況下,它的「FullName」,基本上由我的Doctors表中的FirstName和Surname組成。

無論如何,如果你的誦讀困難跟我一樣只是看一下下面的代碼,並挑選它拆開:)

控制器:

ViewData["docFullDetails"] = 
      new SelectList((from s in db.Doctors 
          select new 
          { 
           FullName = (s.FirstName + s.Surname), 
           ID = s.ID 
          }), 
      "ID", 
      "FullName"); 

的觀點:

<div class="form-control"> 
       @Html.DropDownList("docFullDetails") 
       @Html.ValidationMessageFor(model => model.Doctor) 

      </div> 
0

看起來你可能需要做這樣的事情(它會給下拉「文檔測試」的ID):

@Html.DropDownList("docTest", (SelectList)ViewBag.DoctorOLD, "Place Holder Text", new { @class = "someClass" }) 

但您可能需要不同的功能,我無法從跟提供的代碼量。如果需要將下拉值綁定到模型屬性,也可以使用@html.DropDownListFor進行調查。

This SO post here做了一個很好的解釋html助手下拉列表。

0

你是不是綁定的值砸下來list.Use下面的代碼來獲取值

@Html.DropDownList("docTest",(IEnumerable<SelectListItem>)ViewBag.DoctorOLD) 
0

在你的控制器中,你應該填充你的下拉列表,如

ViewBag.Doctors = new SelectList(db.Doctors.Select(d => new { d.Id, FullName = d.FirstName + " " + d.LastName }), 
       "Id", "FullName"); 

然後在視圖中創建剃刀下拉

@Html.DropDownList("DoctorId", ViewBag.Doctors as SelectList, "Select doctor...", new { @class = "form-control" }) 

您爲您創建大概應該有哪些你是從下拉結合DoctorId物業形態模型

@Html.DropDownListFor(m => m.DoctorId, ViewBag.Doctors as SelectList, "Select doctor...", new { @class = "form-control" }) 
相關問題