Can someone help me into this?
I am still new to MVC, now I've a controller named 'A' and called its view.
What I want to happen is that I want to redirect from A's view to another controller for example controller 'B'.
Is that possible to happen?
Please help.
Thanks
Jason
You can use the RedirectToAction method
MSDN example
public ActionResult LogOff() {
FormsAuth.SignOut();
return RedirectToAction("Index", "Home");
}
First parameter is the Action name and second is the controller name.
Related
I'm Just confused while creating controller in code igniter.
I have two main modules in my project.
menu in header section.
contact us form.
for menu i have created one controller naming devot in devot class i have called all the views for menu, now for contact us form i should go with same controller or i should create new controller.
I'm bit of confused on selection of controllers, how many controller should be there in one application. With only one controller can we complete one application is that possible. ???????????
example:
class SampleController xtends CI_Controlle{
public functin index(){
$this->load->view('header.php');
$this->load->view('yourpage.php');
}
public function page1(){
$this->load->view('header.php');
$this->load->view('yourpage1.php');
}
}
codeigniter Read the Manual
refer
I am trying to create a search form in my MVC application in View 1. The form was working very well when I used to submit the form to same page (View1). That way I can have two ActionResults - one of which accepts HttpPost requests. Everything is cool here
Now things have become slighly complex and I wish to separate the views. So in View1 there is a form and I wanted the results to be displayed in View2. So how do I call ActionResult of View2 from a form in View1?
In short - User enters keyword in View1. Hits Enter. Form in View 1 calls View2. ActionResults in View2 calls some logic to search and return View2 as the view and then I can display the results.
I tried some basic things like action="/View2" but I was pretty sure it would fail. It says 'the resource cannot be found'. Is it even possible to do this? Kindly advice.
*UPDATE*
It can be solved as answered below.
use Html.BeginForm(Name of the ActionResult,Name of the controller)
You need to specify the URL of the other action in the form, preferably by calling the Html.Form("ActionName", "ControllerName") helper.
Using MVC2
Have a master-page that needs to hide certain menus if currently logged in user does not have correct flags set.
Seems like a common problem. Found examples that require that all controllers inherit from a base controller (I have that) and where in constructor of the base controller, the passing of certain parameters to ViewData can occur. This is great and would be easy for me to do, but User.Identity and Request objects are NULL during the construction of base controller.
How do I get to User.Identity of the currently logged in user so that I can query database & modify the ViewData collection accordingly before Master Page view is rendered?
Thanks
You could use child actions along with the Html.Action and Html.RenderAction helpers. So you could have a controller action which returns a view model indicating the currently logged in user info:
public MenuController: Controller
{
public ActionResult Index()
{
// populate a view model based on the currently logged in user
// User.Identity.Name
MenuViewModel model = ...
return View(model);
}
}
and have a corresponding strongly typed partial view which will render or not the menus. And finally inside the master page include the menus:
<%= Html.Action("Index", "Menu") %>
This way you could have a completely separate view model, repository and controller for the menu. You could still use constructor injection for this controller and everything stays strongly typed. Of course there will be a completely different view model for the main controller based on the current page. You don't need to have base controllers or some base view model that all your action should return.
How can I pass data from action in controller to view in another controller?
From action in controler to view in the same controller is easy:
I simply write in action's function :
$this->view->assign('error', 'Wrong login');
and in view I recieve it in this way:
<?=$this->escape($this->error);?>
but how can I do it to receive it in view of another controller?
I might be wrong but my guess is that for every request there is only one Zend_View, so if you set something on ControllerA and forward execution to ControllerB you could access that data in the same way.
It´ll not work if you use the action helper _redirect because it´s a browser redirection, to just forward execution to another "place" use the _forward helper instead.
Another option is the flashMessenger helper, that a look at the docs
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
From a header.php layout we can give instructions based on a given controller by doing:
$this->controller === "mycontrollerhere";
Can we do more or less the same but with a specific controller Action ?
Thanks in advance,
MEM
Not totally sure what you're after but the front controller request object contains the names of the requested module, controller and action.