HtmlHelper類在命令System.Web.Mvc.Html之中,主要由7個靜態類組成,它們分別是FormExtensions類,InputExtensions類,LinkExtensions類,SelectExtensions類,TextExtensions類,ValidationExtensions類,RenderPartialExtensions類。
為了方便開發者使用HtmlHelper控件,在視圖ViewPage類中設置了一個屬性Html它就是HtmlHelper類型。
一.FormExtensions類
定義了3中類型的擴展方法BeginForm,BeginRouteForm,EndForm。
(1) BeginForm (實現表單定義的開始部分)
重載方法有13個:
BeginForm();
BeginForm(Object routeValues);
BeginForm(RouteValueDictionary routeValues);
BeginForm(string actionName,string controllerName);
BeginForm(string actionName,string controllerName,object routeValues);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues);
BeginForm(string actionName,string controllerName,FormMethod method);
BeginForm(string actionName,string controllerName,object routeValues,FormMethod method);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeVaues,FormMethod method);
BeginForm(string actionName,string controllerName,FormMethod method,object htmlAttributes);
BeginForm(string actionName,string controllerName,FormMethod method,IDictionary<string,object> htmlAttributes);
BeginForm(string actionName,string controllerName,object routeValues,FormMethod method,object htmlAttributes);
BeginForm(string actionName,string controllerName,RouteValueDictionary routeValues,FormMethod method,IDictionary<string,object> htmlAttributes);
對于第二個重載方法可以設置如下:
代碼如下:
Html.BeginForm(new{action="action",controller="actroller",id="2"});
在上述代碼中,設置了路由值的一個實例化對象,輸出的HTML語句是:
代碼如下:
<form action="actroller/action/2" method="post"/>
對于最后一個第十三個方法的最后一個參數是實例化對象設置相關屬性的值例如class,width等。
(2)BeginRouteForm (主要實現表單定義的開始部分,以路由的方法設置action的值)
有12個重載方法:
BeginRouteForm(object routeValues);
BeginRouteForm(RouteValueDictionary routeValues);
BeginRouteForm(string routeName);
BeginRouteForm(string routeName,object routeValues);
BeginRouteForm(string routeName,RouteValueDictionary routeValues);
BeginRouteForm(string routeName,FormMethod method);
BeginRouteForm(string routeName,object routeValues,FormMethod method);
……
對于第一個重載方法:
代碼如下:
Html.BeginRouteForm(new {action="action"});
代碼如下:
<form action="Home/action" method="post"/>Home是頁面所在的目錄
新聞熱點
疑難解答
圖片精選