问题:如何修改或创建自己的Html.ActionLink助手,以接受和处理作为空字符串/空字符串传入的第一个参数(linkText)?
详细信息:I当前有一个强类型视图,该视图传递了一个包含搜索结果的模型。我的视图循环遍历模型中的每一项,并尝试使用以下代码显示指向联系人的链接:
@Html.ActionLink(currentItem.ContactName, "contact", "details", New With { .id = currentItem.ContactID }, Nothing)通常情况下,这会很好,但并不是我搜索结果中的每一项都有一个ContactName。当第一个参数为空时,Html.ActionLink助手错误。如果有帮助,下面是ContactName的模型属性(它首先由数据库生成的模板生成,因此我不认为它可以修改):
Public Property ContactName As String我希望有一个助手函数,如果ContactName是空字符串/空,它就不会返回任何内容。
我猜我需要扩展这个助手,而且我很难在VB.net中找到任何用于扩展助手函数的好的、最新的资源。如果其他方法被认为是最佳实践,则它们是非常受欢迎的。我在VB.net、MVC3和Razor中使用ASP.net 4.0框架。提前感谢您的帮助!
发布于 2011-06-22 14:47:46
为了实现这一点,我在我的解决方案中创建了一个Helpers文件夹,并在这里添加了一个新的模块HtmlHelperExtensions.vb (感谢Darin的模块代码):
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“(注意最后一个add命名空间标记):
<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助手工作。
发布于 2011-06-21 19:33:29
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)发布于 2014-12-19 11:34:19
如果您想在MVC视图中使用@Html.ActionLink获得这样的结果:
<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 !");"
})https://stackoverflow.com/questions/6430888
复制相似问题