开发者

call function in controller from view

开发者 https://www.devze.com 2022-12-23 02:28 出处:网络
i wrote a simple function in controller public string LinkProjectSquareFilter(int squareId) { return squareId.ToString();

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


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.


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 ...

0

精彩评论

暂无评论...
验证码 换一张
取 消