ImageActionLink Newbe Redux

发布于 2024-12-05 08:37:13 字数 1050 浏览 3 评论 0 原文

我已经绞尽脑汁好几个小时了,所以我会问...

在 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 帮助器)从未在此处任何帖子中的视图调用中引用。

当我构建与上面相同的调用时,我机器上的编译器抱怨缺少参数。

我缺少一些东西。第一个参数是如何传递或解析的?

谢谢。

I've been beating my head for hours now, so I'll ask...

In all examples for ImageActionBuilders the method looks like:

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(); 
        } 
    } 

I created a class library, included the method, referenced it in my project and I can see it.

The call in the View (.chtml) is documented like this:

@jax.ImageActionLink("../../Content/Images/button_add.png", "JobTasksNew", "TrackMyJob",new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "tmjDynamic" })) 

Being new to C#, the first parameter (this Ajaxhelper helper) is never referenced in the call from the View in any of the posts here.

The compiler on my machine is complaining about missing a parameter when I structure a call identical to the one above.

I'm missing something. How is the first parameter getting passed or resolved?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-12-12 08:37:13

如果您是 C# 新手,那么在开始 ASP.NET MVC 之前您应该做的第一件事就是阅读 C# 中的扩展方法。它们是什么以及它们如何工作。实际上,这是我可以给您的个人建议:在进入 ASP.NET MVC 之前先学习 C#/VB.NET。这就像在没有发现内燃机的情况下试图制造一辆汽车一样。

这就是 ASP.NET MVC 中的 HTML 帮助器:扩展方法。在您的示例中,您已在 ImageActionLinkHelper 静态类中定义了 ImageActionLink 扩展方法。假设此类在 Foo 命名空间中定义:

namespace Foo
{
    public static class ImageActionLinkHelper 
    { 
        public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, object routeValues, AjaxOptions ajaxOptions) 
        {
            ...
        }
    }
}

您应该将此 Foo 命名空间带入视图内的范围,以便您的扩展方法可用:

@using Foo
...
@Ajax.ImageActionLink("../../Content/Images/button_add.png", "JobTasksNew", "TrackMyJob",new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "tmjDynamic" })) 

或者如果您希望它要在 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 the ImageActionLinkHelper static class. Supposing that this class is defined in the Foo namespace:

namespace Foo
{
    public static class ImageActionLinkHelper 
    { 
        public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string actionName, object routeValues, AjaxOptions ajaxOptions) 
        {
            ...
        }
    }
}

You should bring this Foo namespace into scope inside the view so that your extension method is available:

@using Foo
...
@Ajax.ImageActionLink("../../Content/Images/button_add.png", "JobTasksNew", "TrackMyJob",new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "tmjDynamic" })) 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文