How to use custom helper for Tools controls ?
Description
You can render For controls by using the custom HTML helper. Custom HTML helper is used to reduce the amount of tedious typing of HTML tags that you must perform to create a standard HTML page. You can render multiple control on a page by using the custom helper.
Solution
Render DatePicker control by using Custom HTML Helper
1)Using common custom Html Helpers for all the views by placing it in App_Code folder, we suggest you to add the controls by using the corresponding page view.
2)Define the variable in Model page as shown below,
[VB]
Public Class DateSettings Public Property StartDate() As String Get Return mStartDate End Get Set(ByVal value As String) mStartDate = value End Set End Property Private mStartDate As String End Class
3)You need to refer the following namespace in custom helper page,
[VBHTML]
@Import System.Web.Mvc.Html @Import System.Web.Mvc
4)Now, Define the helper using the WebPageView element so that the corresponding page view’s scripts references will be inherited and you can add the required controls to the helper in custom helper page which is in App_Code folder.
[VBHTML]
@Helper MyHelper(ByVal page As WebViewPage, ByVal model As NumericVB.DateSettings) @page.Html.EJ().DatePickerFor(Function(x) model.StartDate) End Helper
5)Passing the model to the view page in the controller as shown below,
[VB]
Function Index() As ActionResult Dim dateSettings As New DateSettings() dateSettings.StartDate = DateTime.Now.AddDays(-12) Return View(dateSettings) End Function
6)Add the helper created and pass the PageContext.Page to get the corresponding page element in view page.
[VBHTML]
@ModelType NumericVB.DateSettings @CustomHelper.MyHelper(PageContext.Page, Model)
Similarly, we can render all our Controls by using custom helper.