从一个控制器到另一个控制器?
From one Controller to another?
我有一个 ShopController 和一个调用 CheckOut 的 Action 方法。我在 Action 方法之前有 [Authorize],但是如果我希望用户在成功登录后继续到另一个控制器(如 CustomersController 和 Create View),以便客户可以将地址和其他信息添加到表单中怎么办?
一种选择是在 ShopController 中创建一个带有表单的新视图,但我想这不是一个好的选择!?
One option would could be to create a new View with a form within the
ShopController, but I guess that isn't a good option!?
没有。这就是您想要的方式。为应用程序的不同 functional/logical 部分创建操作 methods/controllers。在这种情况下,读取地址是一个单独的功能,因此应该属于一个单独的控制器操作方法。
您可以创建一个新的 CustomerController
并在其中添加一个 CreateAddress
操作方法(和视图),并在验证用户(登录?)的操作方法中使用 RedirectToAction
方法根据需要向用户发送您的新视图。
public ActionResult Login(string userName,string password)
{
// do your credential verification logic here
return RedirectToAction("CreateAddress","CustomerController");
}
我有一个 ShopController 和一个调用 CheckOut 的 Action 方法。我在 Action 方法之前有 [Authorize],但是如果我希望用户在成功登录后继续到另一个控制器(如 CustomersController 和 Create View),以便客户可以将地址和其他信息添加到表单中怎么办?
一种选择是在 ShopController 中创建一个带有表单的新视图,但我想这不是一个好的选择!?
One option would could be to create a new View with a form within the ShopController, but I guess that isn't a good option!?
没有。这就是您想要的方式。为应用程序的不同 functional/logical 部分创建操作 methods/controllers。在这种情况下,读取地址是一个单独的功能,因此应该属于一个单独的控制器操作方法。
您可以创建一个新的 CustomerController
并在其中添加一个 CreateAddress
操作方法(和视图),并在验证用户(登录?)的操作方法中使用 RedirectToAction
方法根据需要向用户发送您的新视图。
public ActionResult Login(string userName,string password)
{
// do your credential verification logic here
return RedirectToAction("CreateAddress","CustomerController");
}