2011-12-03 52 views
3

我希望在我的視圖中有一個顯示患者ID,名字和姓氏的下拉列表。使用下面的代碼,它會顯示每個患者的名字。我如何將所有三個屬性都傳遞到viewbag中並讓它們顯示在下拉列表中?MVC3下拉列表,顯示每個項目的多個屬性

控制器

public ActionResult Create() 
      {ViewBag.Patient_ID = new SelectList(db.Patients, "Patient_ID", "First_Name"); 
      return View(); 
      } 

查看

<div class="editor-field"> 
       @Html.DropDownList("Patient_ID", String.Empty) 
       @Html.ValidationMessageFor(model => model.Patient_ID) 
      </div> 

感謝。

好的,我編輯了我的代碼,如下所示,並且收到錯誤消息「沒有ViewData項的類型爲」IEnumerable「,其中包含」SelectedPatientId「鍵。

Controller 


    public ActionResult Create() 
      { 
       var model = new MyViewModel(); 

     { 
      var Patients = db.Patients.ToList().Select(p => new SelectListItem 
      { 
       Value = p.Patient_ID.ToString(), 
       Text = string.Format("{0}-{1}-{2}", p.Patient_ID, p.First_Name, p.Last_Name) 
      }); 

      var Prescribers = db.Prescribers.ToList().Select(p => new SelectListItem 
    { 
     Value = p.DEA_Number.ToString(), 
     Text = string.Format("{0}-{1}-{2}", p.DEA_Number, p.First_Name, p.Last_Name) 
    }); 

      var Drugs = db.Drugs.ToList().Select(p => new SelectListItem 
      { 
       Value = p.NDC.ToString(), 
       Text = string.Format("{0}-{1}-{2}", p.NDC, p.Name, p.Price) 
      }); 


     }; 
       return View(model); 
      } 

視圖模型

public class MyViewModel 
     { 
      [Required] 
      public int? SelectedPatientId { get; set; } 
      public IEnumerable<SelectListItem> Patients { get; set; } 

      [Required] 
      public int? SelectedPrescriber { get; set; } 
      public IEnumerable<SelectListItem> Prescribers { get; set; } 

      [Required] 
      public int? SelectedDrug { get; set; } 
      public IEnumerable<SelectListItem> Drugs { get; set; } 
     } 

查看

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 

    @Html.DropDownListFor(
     x => x.SelectedPatientId, 
     Model.Patients, 
     "-- Select patient ---" 
    ) 
    @Html.ValidationMessageFor(x => x.SelectedPatientId) 
    <button type="submit">OK</button> 

     @Html.DropDownListFor(
     x => x.SelectedPrescriber, 
     Model.Patients, 
     "-- Select prescriber ---" 
    ) 
    @Html.ValidationMessageFor(x => x.SelectedPrescriber) 
    <button type="submit">OK</button> 
} 
+0

Tha噸錯誤通常意味着您的項目列表爲空 –

回答

1

我建議你不要使用在所有的任何ViewBag和定義視圖模式:

public class MyViewModel 
{ 
    [Required] 
    public int? SelectedPatientId { get; set; } 
    public IEnumerable<SelectListItem> Patients { get; set; } 
} 

,然後讓你的控制器動作填充,並通過這個視圖模型到視圖:

public ActionResult Create() 
{ 
    var model = new MyViewModel 
    { 
     Patients = db.Patients.ToList().Select(p => new SelectListItem 
     { 
      Value = p.Patient_ID.ToString(), 
      Text = string.Format("{0}-{1}-{2}", p.Patient_ID, p.First_Name, p.Last_Name) 
     }); 
    }; 
    return View(model); 
} 
在強類型視圖

,最後顯示的下拉列表:

@model MyViewModel 
@using (Html.BeginForm()) 
{ 
    @Html.DropDownListFor(
     x => x.SelectedPatientId, 
     Model.Patients, 
     "-- Select patient ---" 
    ) 
    @Html.ValidationMessageFor(x => x.SelectedPatientId) 
    <button type="submit">OK</button> 
} 
+0

我不相信我可以使用這種方法。除了包含患者姓名列表的DropDownList之外,我還有其他包含來自其他實體的數據的下拉列表。我的創建視圖是Models.Rx的強類型。 Rx包含Patient,Prescriber和Drug之間的關係。如果我遵循你的方法,我將無法拉入任何不在「MyViewModel」內的東西,這是否正確? – HendPro12

+0

我編輯了我的代碼並粘貼在上面。 – HendPro12

+0

@DanielHendrix,在更新的示例中,您已正確添加了另一個字段來保存預訂者下拉列表。然後在創建視圖中實例化這個視圖模型('var model = new MyViewModel()'),然後查詢數據庫或其他內容,並將結果存儲在某些局部變量中,例如患者,處方藥和藥物,但從未設置您的視圖模型的屬性。你需要設置他們'model.Patients =患者; ......在返回視圖之前。 –

1

簡單的方法來實現這一目標只是通過修改模型類或添加/修改部分類來創建模型的其他屬性

[NotMapped] 
public string DisplayFormat 
{ 
    get 
    { 
     return string.Format("{0}-{1}-{2}", Patient_ID, First_Name, Last_Name); 
    } 
} 
+0

修改模型時,我收到以下錯誤。我使用了數據庫優先方法。錯誤錯誤3004:映射從第561行開始的片段問題:沒有爲屬性Patient.DisplayList在Set Patients中指定映射。 具有密鑰(PK)的實體在以下情況下不會往返: 實體類型爲[CAHODModel.Patient] – HendPro12

+0

對,您應該爲該參數添加前綴[NotMapped]我更新了我的示例。請查看http://msdn.microsoft.com/en-us/data/gg193958 – torm

相關問題