將這些方法保留在服務層中並根據輸入要求調用它。檢查傳遞給操作方法的參數。
public ActionResult List(string regionName,string status)
{
List<Customer> customerList=new List<Customer>();
if((!String.IsNullOrEmpty(regionName)) && (!String.IsNullOrEmpty(status)))
{
customerList=CustomerService.GetCustomersForRegionStatus(regionName,status);
//List all Customers
}
else if(!String.IsNullOrEmpty(regionName))
{
customerList=CustomerService.GetCustomersForRegion(regionName);
}
else if(!String.IsNullOrEmpty(status))
{
customerList=CustomerService.GetCustomersForStatus(status);
}
else
{
customerList=CustomerService.GetAllCustomers();
}
return View(customerList);
}
而且你的觀點會被限定到Customer對象的集合
@model IList<Customer>
@foreach(var cust in Model)
{
<p>@cust.Name</p>
}
假設GetCustomersForRegionStatus
,GetCustomersForRegion
和GetAllCustomers
方法返回Customer對象的列表,並在內部調用不同的數據庫訪問方法,以獲取根據傳遞的參數過濾數據。
這些爲urls請求會給現在不同的結果。
yourcontrollername/list
yourcontrollername/list?regionName=someregion
yourcontrollername/list?status=elite
yourcontrollername/list?regionName=someregion&status=elite
謝謝你的迴應。這正是我所期待的。 – tonyapolis 2012-08-03 12:37:09