ImageActionLink Newbe Redux
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 cal开发者_运维技巧l 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.
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.
精彩评论