带图像的MVC3 ActionLink(但没有MvcFutures)?
我不知道是否有人知道,如果它可以使用任何的“开箱即用” ASP.NET MVC3佣工生成一个“链接按钮” ......我目前使用下列内容:带图像的MVC3 ActionLink(但没有MvcFutures)?
<a class="button" title="My Action" href="@Url.Action("MyAction", "MyController", new { id = item.Id })">
<img alt="My Action" src="@Url.Content("~/Content/Images/MyLinkImage.png")" />
</a>
我我试图避免使用MvcFutures,但即使我能够使用它们,我也不认为有一种扩展方法可以实现这一点。 (我相信在这种情况下,解决办法是推出定制帮手as seen here)
最后,this post也有一个不错的主意,通过CSS处理这个问题,但是这不是我所问...
我使用下面的生成操作链接:
using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Fasterflect;
namespace *.Mvc.Extensions
{
public static class HtmlExtensions
{
#region ActionImage
// href image link
public static string ActionImage(this HtmlHelper helper, string href, string linkText, object htmlAttributes,
string alternateText, string imageSrc, object imageAttributes)
{
var sb = new StringBuilder();
const string format = "<a href=\"{0}\"{1}>{2}</a>";
string image = helper.Image(imageSrc, alternateText, imageAttributes).ToString();
string content = string.IsNullOrWhiteSpace(linkText) ? image : image + linkText;
sb.AppendFormat(format, href, GetAttributeString(htmlAttributes), content);
return sb.ToString();
}
// controller/action image link
public static string ActionImage(this HtmlHelper helper, string controller, string action, string linkText, object htmlAttributes,
string alternateText, string imageSrc, object imageAttributes)
{
bool isDefaultAction = string.IsNullOrEmpty(action) || action == "Index";
string href = "/" + (controller ?? "Home") + (isDefaultAction ? string.Empty : "/" + action);
return ActionImage(helper, href, linkText, htmlAttributes, alternateText, imageSrc, imageAttributes);
}
// T4MVC ActionResult image link
public static string ActionImage(this HtmlHelper helper, ActionResult actionResult, string linkText, object htmlAttributes,
string alternateText, string imageSrc, object imageAttributes)
{
var controller = (string) actionResult.GetPropertyValue("Controller");
var action = (string) actionResult.GetPropertyValue("Action");
return ActionImage(helper, controller, action, linkText, htmlAttributes, alternateText, imageSrc, imageAttributes);
}
#endregion
#region Helpers
private static string GetAttributeString(object htmlAttributes)
{
if(htmlAttributes == null)
{
return string.Empty;
}
const string format = " {0}=\"{1}\"";
var sb = new StringBuilder();
htmlAttributes.GetType().Properties().ForEach(p => sb.AppendFormat(format, p.Name, p.Get(htmlAttributes)));
return sb.ToString();
}
#endregion
}
}
注意,GetAttributeString方法依赖于Fasterflect库,使反射的任务更容易,但如果你喜欢不采取额外的依赖,你可以替换成正反射。
图像帮助器扩展名曾经是MvcContrib的一部分,但似乎已被删除,很可能是因为该功能现在内置于MVC中。无论如何,我已经在下面列出了完整性:
public static class ImageExtensions {
public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes) {
return Image(helper, imageRelativeUrl, alt, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(imageRelativeUrl)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
}
string imageUrl = UrlHelper.GenerateContentUrl(imageRelativeUrl, helper.ViewContext.HttpContext);
return MvcHtmlString.Create(Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing));
}
public static TagBuilder Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(imageUrl)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageUrl");
}
TagBuilder imageTag = new TagBuilder("img");
if (!String.IsNullOrEmpty(imageUrl)) {
imageTag.MergeAttribute("src", imageUrl);
}
if (!String.IsNullOrEmpty(alt)) {
imageTag.MergeAttribute("alt", alt);
}
imageTag.MergeAttributes(htmlAttributes, true);
if (imageTag.Attributes.ContainsKey("alt") && !imageTag.Attributes.ContainsKey("title")) {
imageTag.MergeAttribute("title", (imageTag.Attributes["alt"] ?? "").ToString());
}
return imageTag;
}
}
你看起来相当不错。你应该把它包装在一个通用的html助手中,并且每天给它打电话。我敢肯定还有其他更有趣的方面到您的应用程序比NIT采摘约UI辅助:)
还有很多其他的方面......这只是一小部分是我的观点。所以我确实需要“提取它”,但在此期间我想确保我“不要重复自己”(DRY) – zam6ak 2011-02-25 15:08:12
什么是错的您上面张贴的摘录?它似乎没有太多的粗俗,并且它具有以正常标准方式指定HTML属性的优点。 (title,alt等)而且,无论如何,如果你愿意的话,自己写这样一种扩展方法是非常容易的。 – 2011-02-24 22:11:34
@Kirk Woll现在我所用的东西没有问题,但这不是问题。我只是试图看看是否有更好的方法 – zam6ak 2011-02-24 22:19:44
自定义HTML助手解决方案有什么问题? – 2011-02-25 07:20:06