首先,我是MVC的初学者。
我有一个名为的控制器FinanceController
。我有两个名为StoreEvaluationForm
和SendStoreEvaluationToPdf
的控件视图。我想从第二个视图调用第一个视图的public static
函数,该函数在中定义@functions
。
我想视图在mvc中有隐藏的类。因为我意识到,当我将鼠标悬停在第一个视图的函数上时,VS会显示其名为类的名称_Page_Views_Finance_StoreEvaluationForm_cshtml
空间和名为ASP的命名空间。但是我找不到任何访问另一个视图功能的方法。在第二种视图中,ASP名称空间仅具有其名为的类_Page_Views_Finance_SendStoreEvaluationToPdf_cshtml
。
需要明确的是,视图中的函数是C#函数,而不是javascript函数。它的定义是:
@functions
{
public static string NumberFormatter(double? number, bool percent = false)
{
return number == null ? null : string.Format("{0}{1}", number.Value.ToString("N2"), percent ? "%" : null);
}
}
是的,这可以实现。我在Visual Studio 2015中使用默认的MVC项目对其进行了尝试。
About
视图内容:
@{
ViewBag.Title = "About";
}
@functions
{
public static string DoStuff()
{
return "Content from About view.";
}
}
Index
视图内容:
@{
ViewBag.Title = "Home Page";
}
@_Page_Views_Home_About_cshtml.DoStuff()
... (other stuff)
在Index
视图中,下面_Page_Views_Home_About_cshtml
显示一条红色的波浪线:
名称_Page_Views_Home_About_cshtml在当前上下文中不存在
尽管显示了错误消息,但该应用程序已成功构建,并且About
导航到时我可以从视图中看到该消息/Home/Index
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句