VB.Net,MVC3和Razor-修改ActionLink帮助器

| 问题:如何修改或创建自己的Html.ActionLink帮助器,以接受和处理作为空字符串/什么都不传入的第一个参数(linkText)? 详细信息:我目前有一个强类型视图,该视图已传递了包含搜索结果的模型。我的视图遍历模型中的每个项目,并尝试使用以下代码显示指向联系人的链接:
@Html.ActionLink(currentItem.ContactName, \"contact\", \"details\", New With { .id = currentItem.ContactID }, Nothing)
通常,这可以正常工作,但并不是搜索结果中的每个项目都有一个ContactName。当第一个参数为空时,Html.ActionLink帮助器将出错。如果有帮助,这里是ContactName的模型属性(由于“数据库优先”而从模板生成,因此我认为它不能被修改):
Public Property ContactName As String
我希望有一个辅助函数,如果ContactName是一个空字符串/什么也不返回,什么也不返回。 我猜想我需要扩展此帮助程序,而我一直在努力寻找VB.net中任何好的,最新资源来扩展帮助程序功能。如果其他方法被认为是最佳做法,则将非常受欢迎。我正在ASP.net 4.0框架中的VB.net,MVC3和Razor中工作。在此先感谢您的帮助!     
已邀请:
        为此,我在解决方案中创建了一个Helpers文件夹,并添加了一个新模块HtmlHelperExtensions.vb,在此进行详细说明(感谢Darin Dimitrov提供模块代码):
Imports System.Runtime.CompilerServices

Namespace MyHtmlHelpers
Public Module HtmlHelperExtensions
    \'Function to extend the ActionLink helper
    \'Function will return an empty html string for empty or null linkText values
    <Extension()> _
    Public Function MyActionLink(
            ByVal html As HtmlHelper            , _
            ByVal linkText As String            , _
            ByVal actionName As String          , _
            ByVal controllerName As String      , _
            ByVal routeValues As Object         , _
            ByVal htmlAttributes As Object
        ) As IHtmlString

        If String.IsNullOrEmpty(linkText) Then
            Return MvcHtmlString.Empty
        End If

        Return html.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes)
    End Function
End Module
End Namespace
然后,我必须进入位于解决方案的View文件夹中的Web.Config文件,以将其添加为通用视图名称空间,并添加为namespace = \“ solutionname.namespace \”(注意最后一个添加名称空间标记):
<system.web.webPages.razor>
<host factoryType=\"System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\" />
<pages pageBaseType=\"System.Web.Mvc.WebViewPage\">
  <namespaces>
    <add namespace=\"System.Web.Mvc\" />
    <add namespace=\"System.Web.Mvc.Ajax\" />
    <add namespace=\"System.Web.Mvc.Html\" />
    <add namespace=\"System.Web.Routing\" />
    <add namespace=\"BrokerCRM.MyHtmlHelpers\" />
  </namespaces>
</pages>
然后,为了智能起见,我不得不关闭并重新打开视图(.vbhtml),才能为新的html帮助程序工作。     
        
Module HtmlLinkExtensionsModule
    <System.Runtime.CompilerServices.Extension()> _
    Public Function MyActionLink(html As HtmlHelper, linkText As String, actionName As String, controllerName As String, routeValues As Object, htmlAttributes As Object) As IHtmlString
        If String.IsNullOrEmpty(linkText) Then
            Return MvcHtmlString.Empty
        End If
        Return html.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes)
    End Function
End Module
接着:
@Html.MyActionLink(currentItem.ContactName, \"contact\", \"details\", New With { .id = currentItem.ContactID }, Nothing)
    
        如果您想使用@ Html.ActionLink在MVC视图中获得这种结果:
<a href=\"#\"><i class=\"fa-editico\"></i></a>
请参阅此示例图片并阅读以下内容:https://lh5.googleusercontent.com/-5xihbu8wIkY/VJQMq9Y6foI/AAAAAAAAAK4/LNPlbibLEPo/w506-h281/LINK.JPG 希望该代码示例对您有所帮助,我测试了此代码,并且一切正常。祝好运 :) MVC助手:
   //iElementClassName = <i> - element class 
public static MvcHtmlString ActionLinkCustom(this HtmlHelper htmlHelper, string iElementClassName, string action, string controller, object routeValues, object htmlAttributes)
{
    var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
    //get array of HTML attributes
    var attributes = AnonymousObjectToKeyValue(htmlAttributes);
    //create <a> - Tag
    var anchor = new TagBuilder(\"a\");
    //add <i> tag inside in \"<a> <a/>\" Tag
    anchor.InnerHtml = string.Format(\"<i class=\'{0}\'></i>\", iElementClassName);
    //Make Href attribute 
    anchor.MergeAttribute(\"href\", urlHelper.Action(action, controller, routeValues));
    //add array of attributes
    anchor.MergeAttributes(attributes, true);

    return MvcHtmlString.Create(anchor.ToString());
}

//It helps to generate attribute\'s array 
private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject)
{
    var dictionary = new Dictionary<string, object>();

    if (anonymousObject == null) return dictionary;

    foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
    {
        dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
    }

    return dictionary;
}
MVC视图:
//When user click edit css icon, MVC Controller gets Id and we see Alert message
//fa-edit - This is a CSS class name 

@Html.ActionLinkCustom(\"fa-editico\",\"ActionName\", \"ControllerName\", new { Id = Model.Id},
            new
            {
                title = \"Edit Button\",
                onclick = \"alert(\"It Works !\");\"
            })
    

要回复问题请先登录注册