2011-05-04 79 views
1

我是新來的ASP MVC 3,我有以下問題。我有一個表單發出發送數據到服務器的請求。該表單包含銷售人員的DropDownList,此列表從數據庫填充。其主要作用是這樣的:如何不失去在ASP MVC 3中填充DropDownList的集合?

public ActionResult Index() 
{ 
    var viewModel = new OrderSearchViewModel(); 
    viewModel.SalesPeople = GetSalesPeopleList(); // This queries the DB 
    return View(viewModel); 
} 

而視圖模型看起來是這樣的:

public class OrderSearchViewModel 
{ 
     public string Id { get; set; } 
     public DateTime? StartDate { get; set; } 
     public DateTime? EndDate { get; set; } 
     public string PostCode { get; set; } 
     public int SalesPersonId { get; set; } 
     public string SalesPersonRef { get; set; } 
     public int OrderType { get; set; } 
     public string SerialNo { get; set; } 
     public string CustomerPO { get; set; } 
     public List<SalesPerson> SalesPeople { get; set; } 
} 

因此索引視圖時,我提交的銷售人員列表設置爲空的形式(在指數HttpPost方法),我想顯示與仍然填充的列表相同的視圖。儘管如此,要做到這一點,我將不得不再次查詢數據庫。避免這樣做的最佳做法是什麼?

編輯:

我的帖子指數moethod代碼是這樣的:

[HttpPost] 
public ActionResult Index(OrderSearchViewModel viewModel) 
{ 
    var result = QueryOrders(viewModel); 
    //code update the model with the results 
    return View(viewModel); 
} 
+0

爲什麼你認爲再次查詢數據庫不好?另外 - 這裏有一些很好的介紹.NET MVC - > http://www.mvcconf.com/videos – Dan 2011-05-04 12:16:00

回答

0

使用Ajax提交表單,只是使用JSON返回崗位操作的結果到客戶端 - 這樣的數據庫沒有兩次擊中該列表並且用戶也獲得更平滑的體驗。

+0

看起來是個好主意。但是,我是ASP MVC 3的新手,所以如果你有一個鏈接到某個地方,我可以看到這是如何完成的,我會非常感激。 – groovejet 2011-05-04 11:07:41

+0

這是一個有一些良好實踐的視頻 - 作爲一個自由思考的開發人員,您需要確定您的方案中哪些部分最好 - > http://channel9.msdn.com/Series/mvcConf/mvcConf-2 -Eric-Sowell-Evolving-Practices-in-Using-jQuery-and-Ajax-in-ASPNET-MVC-應用 – Dan 2011-05-04 12:17:29

0

POST索引方法應該從FormCollection中獲取SalesPeople的所有值。 您可以隨時使用Request.FormCollection來查詢所有表單數據。另一種選擇是給你某種緩存DB數據:)。 反正請附上你POST索引方法的代碼

+0

好的,現在在帖子 – groovejet 2011-05-04 12:32:00

0

你不應該在你的OrderSearchViewModel SalesPeople集合,因爲它違反單一責任原則。您應該覆蓋SalesPersonId的EditorTemplate並創建單獨的操作,該操作將負責填充下拉菜單。

這裏是我的應用程序中的代碼,您可以使用它作爲示例。請注意,我的CategoryId屬性具有Guid類型 - 您應該替換Guid?與int ?.

模板覆蓋使用mvcextensions:

public class CreateAccidentCommandMetadata : ModelMetadataConfiguration<CreateAccidentCommand> 
{ 
    public CreateAccidentCommandMetadata() 
    { 
     Configure(x => x.TrackingNumber) 
      .Required(); 

     Configure(x => x.CategoryId) 
      .Required() 
      .DisplayName("Category") 
      .Template(MVC.Accident.Views.EditorTemplates.Category); 
    } 
} 

模板內容:

@model Guid? 
@{Html.RenderAction(MVC.Category.List(ViewData.ModelMetadata, Model));} 

Category.List行動:

public virtual ActionResult List(ModelMetadata modelMetadata, Guid? selected) 
    { 
     ViewData.Model = queryService.GetList(new GetCategoryListQueryContext()) 
      .Select(x => new SelectListItem 
          { 
           Text = x.Name, 
           Value = x.Id.ToString(), 
           Selected = selected.HasValue && x.Id == selected 
          }) 
      .ToList(); 

     //it is important to set up ModelMetadata after model 
     ViewData.ModelMetadata = modelMetadata; 

     return View(); 
    } 

和非常簡單的看法:

@model IEnumerable<SelectListItem> 
@Html.DropDownList(ViewData.ModelMetadata.PropertyName, Model) 
+0

我不完全相信這違反了單一責任,不一定是壞事(不能一次遵循所有的設計原則),儘管在他的情況下這可能是矯枉過正的...... – Dan 2011-05-04 12:21:25

+0

但它簡化了很多東西 - 檢索OrderSearchViewModel可能不關心業務員的行動,你不必應付TempData的保存請求之間的銷售人員。防止第二次數據庫查詢的責任我將其放在基礎設施級別 - 比如NHibernate的二級緩存。 – xelibrion 2011-05-04 12:48:05