views:

198

answers:

2

i wrote a simple function in controller

public string LinkProjectSquareFilter(int squareId)
    {

        return squareId.ToString();
    }

how can i call it from view? it say The name 'LinkProjectSquareFilter' does not exist in the current context

A: 

You could make it static. Then you could call ControllerNameController.LinkProjectSquareFilter(5); everywhere in the project as long as you have the required namespaces included (in a viewfile this is done with a <%@ something tag at the beginning of the file. I don't remember what that something is thought :-P ...

Alxandr
the ASP.NET MVC view doesn't usually know anything about the controller. This violates the correct arrangement of dependencies.
Marek
Still, even though it's bad practice and shouldn't be done, it's still possible this way, and that was what he asked for...
Alxandr
+2  A: 

This kind of method should not be in controller at all. If it is only a simple ToString call, do it in the view directly.

If it is something more complex, do it in your ViewModel (the type you are passing to your strongly typed view) or create an extension method (e.g. as an extension on int type) and call that method from the view directly - but only if it is a simple view related transformation.

If it is a more complex transformation involving any kind of business logic, do that in your controller or in your service layer (used by controller) before passing the data to view.

Marek