2013-05-13 81 views
0

我通過docs獲取數據列表,其中包含當前登錄的用戶有權訪問的每個部門和功能的列表。我需要在View頁面上爲DropDownList填充一個不同的Departments列表和一個DropDownList的不同的列表。我目前甚至不使用docs來做到這一點,但一個不同的LINQ查詢來實現這一點。有沒有一種方法可以使用我傳遞的當前模型?在查看比使用ViewBag更好的方法

var docs = (Long LINQ query that joins in four different tables and returns a model) 

ViewBag.DepartmentList = db.Department.Where(x => (x.name != null)).Select(s => new SelectListItem 
      { 
       Value = s.name, 
       Text = s.name 
      }) 
      .Distinct(); // Fill the viewbag with a unique list of 'Department's from the table. 

ViewBag.FunctionList = db.Function.Where(x => (x.name != null)).Select(s => new SelectListItem 
      { 
       Value = s.name, 
       Text = s.name 
      }) 
      .Distinct(); // Fill the viewbag with a unique list of 'Function's from the table. 

代碼:(強類型的模型)

@model IEnumerable<DB.Models.MasterList> 

@Html.DropDownList("DepartmentList", "Select a Department") 
@Html.DropDownList("FunctionList", "Select a Function") 

回答

3

定義將在您的視圖中使用的模型。

public class MyViewModel 
{ 
    public string SelectedDepartment { get; set; } 
    public string SelectedFunction { get; set; } 


    public IEnumerable<SelectListItem> Departments { get; set; } 
    public IEnumerable<SelectListItem> Functions { get; set; } 

    // Your old model 
    public IEnumerable<MasterList> Master   { get; set;} 

} 

在您的控制器中,填充這些集合並返回您的模型以查看。

[HttpGet] 
public ActionResult ActionMethodName() 
{ 
    var model = new MyViewModel(); 

    model.Departments = db.Departments.Where(x => (x.name != null)) 
          .Select(s => new SelectListItem 
          { 
           Value = s.name, 
           Text = s.name 
          }) 
          .Distinct(); 

    model.Functions = db.Functions.Where(x => (x.name != null)) 
          .Select(s => new SelectListItem 
          { 
           Value = s.name, 
           Text = s.name 
          }) 
          .Distinct(); 

    return View(model); 
} 

在您的視圖中,使用強類型html助手。

@model MyViewModel 

@Html.DropDownListFor(m => m.SelectedDepartment, Model.Departments) 
@Html.DropDownListFor(m => m.SelectedFunction, Model.Functions) 

當你回來後表單服務器,SelectedDepartmentSelectedFunction應該在你的視圖中選擇的值。

+1

我想我明白這個更好一點,謝謝! – 2013-05-13 21:27:19

0

您可以隨時爲您的視圖ViewModel類,並把所有必需的視圖信息在裏面。

您可以使用像AutoMapper(https://github.com/AutoMapper/AutoMapper)這樣的框架來幫助您完成數據庫模型與視圖模型之間的映射(我相信最好視圖根本不知道數據庫模型),並且在模型信息,你也可以添加這些列表(這就是我所做的,我有一個實體的屬性,以及這些列表的屬性)。

如果您在許多視圖中需要此信息,則始終可以創建BaseViewModel並在BaseController中對這些信息進行輪詢。

1

您可以創建一個視圖模型,並把所有這些數據在此視圖模型:

視圖模型

public class MyViewModel{ 
    public object DepartmentList{get; set;} 
    public object FunctionList{get; set;} 
    public IEnumerable<MasterList> Master {get; set;} 
} 

控制器

var docs = (Long LINQ query that joins in four different tables and returns a model) 
MyViewModel vm = new MyViewModel(); 
vm.Master = docs; // I guess docs is a list of Masterlist 
vm.DepartmentList = db.Department.Where(x => (x.name != null)).Select(s => new SelectListItem 
      { 
       Value = s.name, 
       Text = s.name 
      }) 
      .Distinct(); // Fill the viewbag with a unique list of 'Department's from the table. 

vm.FunctionList = db.Function.Where(x => (x.name != null)).Select(s => new SelectListItem 
      { 
       Value = s.name, 
       Text = s.name 
      }) 
      .Distinct(); // Fill the viewbag with a unique list of 'Function's from the table. 
return View(vm); 

查看

@model MyViewModel 

@Html.DropDownList("DepartmentList", "Select a Department") 
@Html.DropDownList("FunctionList", "Select a Function") 
+0

我會給你一個鏡頭,我認爲這有我需要的一切。 – 2013-05-13 21:25:31

相關問題