Creating and calling custom controller actions in asp.net mvc 2 - asp.net-mvc-2

In asp.net mvc 2 web application I've created a controller and default actions work just fine(Index, Display, Create). Now I want to create a custom controller action which takes an object id and named, say, Rotate, I've created a public method which does some logic and redirects to the index page again.
The problem is that when I'm trying to call
<%: Html.ActionLink("Click on me", "Rotate", new { id = item.Id })%>
there's 404 error.
What's the problem? Should I register that created controller action somewhere in order to use it?
EDIT:
public ActionResult Rotate(int id)
{
/* does some stuff to the object */
return RedirectToAction("Index");
}
URL is like this: http://localhost/Home/Rotate/1

If your controller and action match your default route (controller / action / id) than you don't need to do anything else to 'register' your action.
And you need to compile the project ;-)

Related

confusion while creating controller in Code igniter

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

Redirecting from a view to the controller

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.

Passing data from controller to master page - based on currently logged in user

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 do I get the RouteData associated with the parent action in a partial view?

In the action for a PartialView, I'd like to know the RouteData associated with the Parent Action.
This partial view is part of my masterpage template, and I'd like for it to know the Controller and Action that was called as part of the page rendering.
In my PartialView's action, I inspect RouteData.Values["controller"] and RouteData.Values["action"], but I only get the controller and action for my PartialView.
Any suggestions?
--edit--
It looks like off of the ControllerContext (from which ViewContext derives) you can get the ParentActionViewContext:
ViewContext.ParentActionViewContext.RouteData["controller"]

How to disable a view script in a controller - Zend Framework

I am playing with zend framework's MVC. One thing I found out is that Zend Framework tries to include a view script whenever a controller is called. I can disable it in bootstrap with the following code.
$frontController->setParam('noViewRenderer',true);
However, I have to initialize Zend_View class in a controller method then render a script file myself.
How can I stop including a view script in a controller method so I can disable it if only I want to?
you can disable the view renderer controller helper, with this code in your controller:
public function myAction()
{
$this->_helper->viewRenderer->setNoRender(true);
// from now on, ZF won't search for a matching view script file.
}
The best example would be to use both commands above:
public function myAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
First one disables layout,in general is enabled
application.ini
default
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
$this->_helper->layout->disableLayout();
and second disables view script (.phtml) so exception is not thrown if view script file is not found
$this->_helper->viewRenderer->setNoRender(true);
There are also view helpers that include bouth listened abowe and are not necessary, for example you want to return JSON from array data and not render view element.
public function myAction() {
$this->_helper->json(array());
}
will not render layout nor view script.
Easy, just disable it from within your action.
$this->_helper->layout->disableLayout();
If you aren't talking about layouts, then just add an exit() to your action. Just understand what sort of impact that will have on your application.