c# - Accessing a view's function from another view -


firstly, i'm beginner of mvc.

i have controller named financecontroller. have 2 views named storeevaluationform , sendstoreevaluationtopdf of control. want call first view's public static function, defined in @functions, second view.

i guess views have hidden class in mvc. because realized when mouse on first view's function, vs shows class named _page_views_finance_storeevaluationform_cshtml , namespace named asp. couldn't find way accessing view's function. in second view asp namespace has class named _page_views_finance_sendstoreevaluationtopdf_cshtml.

to clear, function in view c# function not javascript function. definition is:

@functions {     public static string numberformatter(double? number, bool percent = false)     {         return number == null ? null : string.format("{0}{1}", number.value.tostring("n2"), percent ? "%" : null);     } } 

yes, can achieved. tried default mvc project in visual studio 2015.

content of about view:

@{     viewbag.title = "about"; }  @functions {     public static string dostuff()     {         return "content view.";     } } 

content of index view:

@{     viewbag.title = "home page"; }  @_page_views_home_about_cshtml.dostuff()  ... (other stuff) 

in index view there red squiggly line below _page_views_home_about_cshtml displaying :

the name _page_views_home_about_cshtml not exist in current context

despite error message, application builds , can see message about view when navigating /home/index.

view


Comments