2
A
回答
2
我真的不知道为什么一个扩展方法是不适合的,但这样的事情应该工作:
@helper ActionImage(string action, object routeValues, string imagePath, string alt) {
<a href="@Url.Action(action, routeValues)">
<img src="@Url.Content(imagePath)" alt="@alt">
</a>
}
这只是从我的头顶,让您的milage可能会有所不同。您还应该能够将问题中提供的实现用作@functions { }
块,而不是扩展方法。
0
这里是我的形象HTML辅助的简单例子
关于HTML辅助小文章,以及如何分类:G642.44它在HTML辅助的另一个
http://www.sexyselect.net/blog/post/2011/08/16/Writing-a-Razor-MVC3-HTML-Helpers
例如 http://www.aspnetwiki.com/page:creating-custom-html-helpers
示例代码
/// <summary>
/// Insights the traffic light image.
/// </summary>
/// <param name="html">The HTML.</param>
/// <param name="trafficLight">The traffic light.</param>
/// <returns>Image for the current traffic light. If not recognised writes name ot he light.</returns>
public static MvcHtmlString InsightTrafficLightImage(this HtmlHelper html, TrafficLight trafficLight)
{
StringBuilder result = new StringBuilder();
string color = string.Empty;
string hoverText = string.Empty;
switch (trafficLight)
{
case TrafficLight.Amber:
{
color = "Yellow";
hoverText = "Work in progress";
break;
}
case TrafficLight.Green:
{
color = "green";
hoverText = "Complete";
break;
}
case TrafficLight.Red:
{
color = "red";
hoverText = "Not yet started";
break;
}
case TrafficLight.Black:
case TrafficLight.Unknown:
default:
{
break;
}
}
if (!string.IsNullOrEmpty(color))
{
TagBuilder img = new TagBuilder("img");
img.MergeAttribute("src", string.Format("/Content/images/traffic_light_{0}.gif", color));
img.MergeAttribute("alt", hoverText);
img.MergeAttribute("title", hoverText);
result.Append(img.ToString());
}
else
{
result.Append(Enum.GetName(typeof(TrafficLight), trafficLight));
}
return MvcHtmlString.Create(result.ToString());
}
希望你找到它有用
+0
我试图找到帮助方法的解决方案..类似的方法描述在引用的链接,但我没有使用@帮助方法.. – Ante 2012-02-10 10:33:16
相关问题
- 1. 创建MVC3 Razor Helper像Helper.BeginForm()
- 2. MVC3 RC Razor @helper位置
- 3. 使用Razor MVC3的发送方 - 接收方
- 4. 动作链接不MVC3
- 5. 图像链接无法在移动设备上使用
- 6. 链接不是在ASP MVC3 Razor视图正确显示
- 7. MVC3 - Helper方法被几个控制器使用
- 8. 使用.next()方法更新灯箱中的图像链接
- 9. 超链接调用动作方法
- 10. 无法使图像的链接
- 11. 使用pyimgur将图像上传到imgur,使用图像链接
- 12. 使用Node/Nightwatch自动点击图像映射动作/链接
- 13. 使用的link_to使图像的链接
- 14. 浏览器在MVC3 Razor上的图像上扮演着不同的角色
- 15. 与动作链接传递参数的图像
- 16. 使用许多型动物主题相同MVC3 Razor视图
- 17. MVC3负载动态属性到JavaScript使用Razor视图引擎
- 18. 如何在mvc3中使用razor语法测试视图?
- 19. MVC3 Razor提供链接到内存中的文件(不在磁盘上)
- 20. XmlDocument的helper方法
- 21. Helper方法Rails的
- 22. 在MVC3 Razor视图引擎
- 23. 使用带链接的MVC3 AntiforgeryToken?
- 24. ASP.NET MVC Razor - 链接共享 - 增加图像大小
- 25. 我的链接不会在我的图标图像上工作
- 26. 在使用ASP.NET MVC的App_code文件夹中使用razor @helper 3
- 27. Razor中的条件链接
- 28. 使用图像作为链接悬停图像
- 29. 使用javascript指定图像的链接
- 30. ASP.NET MVC3中的动态表格Razor
我刚刚Google了它!我不知道从哪里开始。由于varius依赖关系,此解决方案不能只在辅助方法中复制。 – Ante 2012-02-10 01:30:05