从视图调用控制器中的函数
我在控制器中编写了一个简单的函数,
public string LinkProjectSquareFilter(int squareId)
{
return squareId.ToString();
}
如何从视图中调用它?它说名称“LinkProjectSquareFilter”在当前上下文中不存在
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种方法根本不应该出现在控制器中。如果只是简单的
ToString
调用,则直接在视图中执行。如果它是更复杂的东西,请在您的 ViewModel(您传递给强类型视图的类型)中执行它或创建一个 扩展方法(例如,作为
int
类型的扩展)并直接从视图调用该方法 - 但前提是它是一个简单的视图相关转换。如果它是涉及任何类型的业务逻辑的更复杂的转换,请在将数据传递给视图之前在控制器或服务层(由控制器使用)中执行此操作。
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.
你可以让它静态。然后,只要包含所需的命名空间,您就可以在项目中的任何位置调用
ControllerNameController.LinkProjectSquareFilter(5);
(在视图文件中,这是通过<%@ Something
完成的)文件开头的 code> 标记我不记得那是什么东西了:-P ...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 ...