@helper: You can have a return value or no return value @function: A return value is required to extract the code from the public part of the view into a separate method
Public part ViewThe view of the extracted public part must be placed in the App_Code directory, with the file name xxx.cshtml. The filename is the class namecommonui.cshtml
@helper Showcustomerinfo (Customer customer) {<ul> <li> @customer. Companyname</li> <li> @customer. Customerid</li> </ul>}
@helper mutiply (int a,int b) {var r = a * b; @r;} @functions {public static ihtmlstring GetCurrentTime () {return new htmlstring (DateTime.Now.ToString ("yyyy-mm -DD hh:MM:ss ")); }}
code in Models:Namespace step1{public class Customer {public string CustomerID {get; Set } public string CompanyName {get; Set } }}
the code in the Controller:Using system;using system.collections.generic;using system.linq;using system.web;using SYSTEM.WEB.MVC; Namespace step1.controllers{public class Commonuicontroller:controller {////GET:/commonui/ Public ActionResult Helper () {Customer c = new Customer () {companyname= "Redwave", CustomerID = "hbb0b0"}; return View (c); } }}
the code in View:@using step1.app_code;@{viewbag.title = "Helper"; Layout = "~/views/shared/_layout.cshtml";}
The use of @helper and @function in Razor