ImageActionLink Newbe Redux
我已经绞尽脑汁好几个小时了,所以我会问...
在 ImageActionBuilders 的所有示例中,该方法如下所示:
public static class ImageActionLinkHelper
{
public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, object routeValues, AjaxOptions ajaxOptions)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", imageUrl);
builder.MergeAttribute("alt", "");
var link = helper.ActionLink(builder.ToString(TagRenderMode.SelfClosing), actionName, routeValues, ajaxOptions);
return link.ToHtmlString();
}
}
我创建了一个类库,包含该方法,在我的项目中引用它,我可以看到它。
视图 (.chtml) 中的调用记录如下:
@jax.ImageActionLink("../../Content/Images/button_add.png", "JobTasksNew", "TrackMyJob",new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "tmjDynamic" }))
对于 C# 来说,第一个参数(此 Ajaxhelper 帮助器)从未在此处任何帖子中的视图调用中引用。
当我构建与上面相同的调用时,我机器上的编译器抱怨缺少参数。
我缺少一些东西。第一个参数是如何传递或解析的?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您是 C# 新手,那么在开始 ASP.NET MVC 之前您应该做的第一件事就是阅读 C# 中的扩展方法。它们是什么以及它们如何工作。实际上,这是我可以给您的个人建议:在进入 ASP.NET MVC 之前先学习 C#/VB.NET。这就像在没有发现内燃机的情况下试图制造一辆汽车一样。
这就是 ASP.NET MVC 中的 HTML 帮助器:扩展方法。在您的示例中,您已在
ImageActionLinkHelper
静态类中定义了ImageActionLink
扩展方法。假设此类在Foo
命名空间中定义:您应该将此
Foo
命名空间带入视图内的范围,以便您的扩展方法可用:或者如果您希望它要在 ASP.NET MVC 3 应用程序的所有 Razor 视图中可用,您应该在
~/Views/web.config
的
部分中添加 Foo 命名空间文件。If you are new to C# the first and foremost thing that you should do before jumping into ASP.NET MVC is to read about extension methods in C#. What they are and how they work. Really it's a personal advice I can give you : learn C#/VB.NET before jumping into ASP.NET MVC. It's like trying to build a car without having discovered the combustion engine.
That's what HTML helpers in ASP.NET MVC are: extension methods. In your example you have defined the
ImageActionLink
extension method inside theImageActionLinkHelper
static class. Supposing that this class is defined in theFoo
namespace:You should bring this
Foo
namespace into scope inside the view so that your extension method is available:or if you want it to be available in all Razor views of your ASP.NET MVC 3 application you should add the Foo namespace inside the
<namespaces>
section of the~/Views/web.config
file.