(MVC) MVC (2018)

How to create Razor html-helper in VB.NET

In my site you may see many extension useful function, for example Amazing extension function CopyLinqDataMembersByName to expand Linq-to-SQL, but I still have no description about writing HTML-helper. Process of creating HTML-helper has some difference comparing extension of other type. In other side Microsoft package of HTML-helper is so restricted, for example even is absent HTML=helper for creating Img tag with link wrapper. This helper was presented in ASP NET since 2002 year, and has name ImgLink, but each new version of Visual Studio is more difficult, more slowly and more restricted for programmer opportunity comparing to previous.

For creating MVC-helper fistly please add this code to project.

   1:  Imports System.Web.Mvc
   3:  Public Module HtmlHelperExtension1
   5:      <Runtime.CompilerServices.Extension()>
   6:      Public Function ImageLink(ByVal htmlHelper As HtmlHelper, ByVal imgSrc As String, ByVal alt As String, ByVal actionName As String, ByVal controllerName As String, ByVal routeValues As Object, ByVal htmlAttributes As Object, ByVal imgHtmlAttributes As Object) As MvcHtmlString
   7:          Dim urlHelper As UrlHelper = (CType(htmlHelper.ViewContext.Controller, Controller)).Url
   8:          Dim imgTag = New TagBuilder("img")
   9:          imgTag.MergeAttribute("src", imgSrc)
  10:          If (imgHtmlAttributes IsNot Nothing) Then imgTag.MergeAttributes(AnonToDictionary(imgHtmlAttributes), True)
  11:          Dim url As String = urlHelper.Action(actionName, controllerName, routeValues)
  12:          Dim imglink = New TagBuilder("a")
  13:          imglink.MergeAttribute("href", url)
  14:          imglink.InnerHtml = imgTag.ToString()
  15:          If (htmlAttributes IsNot Nothing) Then imglink.MergeAttributes(AnonToDictionary(htmlAttributes), True)
  16:          Return MvcHtmlString.Create(imglink.ToString())
  17:      End Function
  19:      <Runtime.CompilerServices.Extension()>
  20:      Public Function AnonToDictionary(ByVal data As Object) As IDictionary(Of String, Object)
  21:          Dim publicAttributes As System.Reflection.BindingFlags = System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.Instance
  22:          Dim dictionary As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
  23:          For Each [property] As System.Reflection.PropertyInfo In data.[GetType]().GetProperties(publicAttributes)
  24:              If [property].CanRead Then dictionary.Add([property].Name, [property].GetValue(data, Nothing))
  25:          Next
  26:          Return dictionary
  27:      End Function
  28:  End Module

This code has one interesting point - funtion to transform anonymous object to dictionary, this is need because from Razor we transfer parameters in anonymous type. Firstly I try to create MVC-helper in simple way, without reflection but failure.

After that I use another way with System.Reflaction. If you have more interesting about practical use System.Reflaction you may see another my articles for this theme, for example Аналіз MVC-сайту за допомогою System.Reflection.

And secondary, need to import on page this extension.

My helper create HTML below. Tag </img> is odd, but has no influence to processing html in browser.

More Define common code as Function and Helpers (directly in View and in the codeBehind), Додаток про загальний код у проектах MVC.

Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21
Link to this page: //www.vb-net.com/RazorHtmlHelper/index.htm