Asp.net MVC模式、控制器方法和;格特比…“;
我对使用MVC模式比较陌生,这是我的第一个问题;我专门使用ASP.NETMVC3,但我的问题可能适用于一般的MVC模式。重用基本上返回相同视图但可能从数据库查询不同结果集的控制器方法的最佳方法是什么?例如,我可能希望显示所有客户,或特定地区的一些客户,或具有“精英”状态的一些客户Asp.net MVC模式、控制器方法和;格特比…“;,asp.net,asp.net-mvc-3,controller,Asp.net,Asp.net Mvc 3,Controller,我对使用MVC模式比较陌生,这是我的第一个问题;我专门使用ASP.NETMVC3,但我的问题可能适用于一般的MVC模式。重用基本上返回相同视图但可能从数据库查询不同结果集的控制器方法的最佳方法是什么?例如,我可能希望显示所有客户,或特定地区的一些客户,或具有“精英”状态的一些客户 我目前为每个“GetBy…”结果集都有单独的控制器方法。有没有办法使用“列表”控制器并用不同的结果集填充它?也许通过将结果集作为参数注入?将这些方法保留在服务层中,并根据输入需求调用它。检查传递给action方法的参数
我目前为每个“GetBy…”结果集都有单独的控制器方法。有没有办法使用“列表”控制器并用不同的结果集填充它?也许通过将结果集作为参数注入?将这些方法保留在服务层中,并根据输入需求调用它。检查传递给action方法的参数
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);
}
谢谢你的回复。这正是我想要的。
@model IList<Customer>
@foreach(var cust in Model)
{
<p>@cust.Name</p>
}
yourcontrollername/list
yourcontrollername/list?regionName=someregion
yourcontrollername/list?status=elite
yourcontrollername/list?regionName=someregion&status=elite