[英]Routing from one view to another controller's action method and passing a parameter to that controller's constructor
我正在創建我的第一個C#MVC網站,但很早以前,我遇到了一個障礙,在那我不確定是否要以完全錯誤的方式進行事情,並且找不到與我自己的在線網站類似的示例,但是看來我要做的事情應該很簡單。
基本上,我有一個初始控制器(稱為ClientController),該控制器設置了一個客戶端列表,然后在列表視圖中顯示它們:
public class ClientController : Controller
{
private readonly IClientManagerRepository _clientManagerRepository;
public ClientController()
: this(new EntityClientManagerRepository())
{
}
public ClientController(IClientManagerRepository repository)
{
_clientManagerRepository = repository;
}
//
// GET: /Client/
public ViewResult List()
{
return View(_clientManagerRepository.GetAllClients());
}
}
然后在我看來,我有一個操作鏈接,該路由要路由到我的UserController,並向其傳遞客戶端名稱,以便它可以為該特定客戶端構建用戶列表。
@Html.ActionLink("View Admin Users","Index","User",new {clientName = item.ClientName},null)
這適用於以下代碼:
public class UserController : Controller
{
private IUserManagerRepository _userManagerRepository;
//
// GET: /User/
public ActionResult Index(string clientName)
{
_userManagerRepository = new EntityUserManagerRepository(clientName);
return View(_userManagerRepository.GetAllUsers());
}
}
並且我的用戶列表在我的視圖中正確顯示。
但是,當我隨后在我的details操作方法中添加該方法時,由於_userManagerRepository
未實例化,因此它不起作用:
//
// GET: /User/Details/5
public ActionResult Details(int contactId)
{
return View(_userManagerRepository.GetUser(contactId));
}
我必須每次都傳入客戶名,然后重新實例化_userManagerRepository。 不過,這並不是一個很好的方法。
理想情況下,我想在UserController的構造函數中創建_userManagerRepository
。 我一直在研究如何做到這一點,所以我會得到一些類似的信息:
public class UserController : Controller
{
private IUserManagerRepository _userManagerRepository;
public UserController(string clientname)
: this(new EntityUserManagerRepository(clientname))
{
}
public UserController(IUserManagerRepository repository)
{
_userManagerRepository = repository;
}
我研究過可以創建自己的控制器工廠,以便可以在userController構造函數中有一個參數,但是我仍然不知道如何將客戶名參數從視圖傳遞給UserController。
如果要在控制器的構造函數中實例化Repository類,則可以使用NInject ,這是一種非常好的方法。
1-從Nuget安裝Ninject 2-創建存儲庫摘要,例如ICustomerRepository
public abstract ICustomerRepository
{
string GetCustomerName();
}
3-創建存儲庫,例如CustomerRepository
public class CustomerRepository:ICustomerRepository
{
string GetCustomerName()
{
return ("John");
}
}
4創建CustomerControllerFactory類
public class CustomControllerFactory : DefaultControllerFactory
{
private static IKernel ninjectKernel;
public CustomControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings(ninjectKernel);
}
protected override IController GetControllerInstance
(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return (new Controllers.MessageController());
}
else
{
return ((IController)ninjectKernel.Get(controllerType));
}
}
public static void AddBindings(IKernel ninjectKernel)
{
Common.DependencyInjection.DependencyManager.GetDependencyInjections().ForEach(current =>
{
if (current.Abstract != null && current.Implementation != null)
{
ninjectKernel.Bind(current.Abstract).To(current.Implementation);
}
});
ninjectKernel.Bind<ICustomerRepository>().To(typeof(CustomerRepository));
}
}
ninjectKernel.Bind()。To(typeof(CustomerRepository));
我用較高代碼將ICustomerRepository綁定到CustomerRepository
5-將以下代碼添加到Application_Start
ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
6-創建新控制器
public class CustomerController:Controller
{
public CustomerController(ICustomerRepository customerRepository)
{
//customerRepository instantiate to CustomerRepostory Class automatically
}
}
我認為對您有用的依賴注入
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.