开发者

mvc 3 Ajax.ActionLink with image not working

I am using vs2010 win7 x64 MVC 3 with regular asp.net development server. And the newest versions of firefox, IE9 and chrome.

I have a strongly typed view and i'm trying to get a ajax extention method working via a slightly modified version of this code asp-net-mvc-ajax-actionlink-with-image that Arjan Einbu provided for mvc 3.

Here is my extension method code located in a Helper.cs file in the App_Code folder of my mvc website.

public static class HelperExtensions
{
    public static IHtmlString ImageActionLink(this AjaxHelper helper, string imageUrl, string titleText, string actionName, object routeValues, AjaxOptions ajaxOptions)
    {
        var builder = new TagBuilder("img");
        builder.MergeAttribute("src", imageUrl);
        builder.MergeAttribute("title", titleText);
        var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString();
        return new MvcHtmlString(link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
    }
}

In my view I have @model IEnumerable<Models.SpeciesType> at the top defining the model and I'm using ...

        @Ajax.ImageActionLink(Url.Content("~/Content/ThumbUp.png"), "Valid/Appropriate", "VoteUp", new {id = item.ID}, new AjaxOptions { HttpMethod = "POST" } ); 

to insert the ajax action link with an image to allow the user to vote on the item. The above code does not work and no matter what I do it keep giving me error after endless error. The current error it is giving me is ...

Compiler Error Message: CS0121: The call is ambiguous between the following methods or pro开发者_高级运维perties: 'HelperExtensions.ImageActionLink(System.Web.Mvc.AjaxHelper, string, string, string, object, System.Web.Mvc.Ajax.AjaxOptions)' and 'HelperExtensions.ImageActionLink(System.Web.Mvc.AjaxHelper, string, string, string, object, System.Web.Mvc.Ajax.AjaxOptions)'

Which means I have two methods with the same name and signature in the same namespace that are conflicting, again impossible because I have only defined it once in one code file. So I thought there might me a caching issue so I did a ctrl+shift+delete in firefox and did a full clear of the cache, history and everything else. Refreshed the page and I get the same error. I also tested it in IE9 and chrome same issue.

I stopped and started the asp.net development server, quit visual studio and restarted still same issue.

Did disk clean up checked everything and ran it. Disk Cleanup did not delete all the files in "C:\Users\Dean\AppData\Local\Temp" So I manually deleted all remaining files in the folder that i could in including the "Temporary ASP.NET Files" folder. Then I ran vs2010 again and ran the website. Again still the same error "CS0121: The call is ambiguous between the following methods ... etc etc"

I can't get rid of this error no matter what I do. This was not the only error I was getting. Before this error started persisting I was also getting errors relating to mvc not being able to find a ImageActionLink method with the right signature namely

ImageActionLink(this AjaxHelper<IEnumerable<SpeciesType>> helper, etc etc

that error I assume was relating to the fact that I am using a strongly typed view.

So to try and get rid of the "call is ambiguous" error I commented out the entire ImageActionLink method and recompiled and ran the website again. I got this error

Compiler Error Message: CS1061: 'System.Web.Mvc.AjaxHelper>' does not contain a definition for 'ImageActionLink' and no extension method 'ImageActionLink' accepting a first argument of type 'System.Web.Mvc.AjaxHelper>' could be found (are you missing a using directive or an assembly reference?)

That error totally makes sense because the ImageActionLink no longer exists. So next thing I did was un-comment the ImageActionLink method recompile and run the website again. And sure enough the same "call is ambiguous" error.

I'm at a loss. I've done everything I can think of to clear various cache's to rule that out, comment and un-comment out pieces of code and test what part breaks. But I'm stuck not being able to get passed the "call is ambiguous" error.

Anyone have any ideas how to fix this "call is ambiguous" error so I can get on to asking why I can't seem to get a simple ajax extension method working ?


I got rid of the "call is ambiguous" error. What happened was I copied Arjan Einbu's mvc 3 version of the code found here asp-net-mvc-ajax-actionlink-with-image.

Then I created a new class in visual studio in the App_Code folder and did a ctrl+a ctrl+v and replaced all the pre generated class template code with just the HelperExtensions class. The problem was that I did not wrap the HelperExtensions with a namespace that matched the website namespace.

Something about how asp.net compiles code files without namespaces was screwing it up. I'm asuming it's probably because the code is compiled in the main website dll at build time and then when asp.net runs it compiles any code files it finds in the app_code folder thus two classes with the same name and same method name or some such.

Anyway now that I have solved that aggravation, I run my website and I get the fallowing error ...

Compiler Error Message: CS1061: 'System.Web.Mvc.AjaxHelper<IEnumerable<Models.SpeciesType>>' does not contain a definition for 'ImageActionLink' and no extension method 'ImageActionLink' accepting a first argument of type 'System.Web.Mvc.AjaxHelper<IEnumerable<Models.SpeciesType>>' could be found (are you missing a using directive or an assembly reference?)

The error reads as though it's finding the ImageActionLink method but the first parameter on the ImageActionLink method is not of the type AjaxHelper<IEnumerable<Models.SpeciesType>>

The error does not quite make sense. Yes the first parameter on the ImageActionLink method is of type AjaxHelper but not of the generic AjaxHelper type. So I tried to modify the ImageActionLink method declaration like so

  public static IHtmlString ImageActionLink(this AjaxHelper<IEnumerable<SpeciesType>> helper, string imageUrl, string titleText, string actionName, object routeValues, AjaxOptions ajaxOptions)

But I still get the same error. The first parameter on ImageActionLink is of type AjaxHelper but it's defined "this AjaxHelper helper" meaning it's a extension method that is supposed to extend the AjaxHelper class.

I don't really need the @model IEnumerable<Models.SpeciesType> at the top of my index.cshtml file so I removed it and tried to run the app again with the same error only this time it's

Compiler Error Message: CS1061: 'System.Web.Mvc.AjaxHelper<dynamic>' does not contain a definition for 'ImageActionLink' and no extension method 'ImageActionLink' accepting a first argument of type 'System.Web.Mvc.AjaxHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?)

Anyone know why I'm getting this error? And how to fix it?


Where in Web.config have you added namespace (you are using razor views) ? To pages or to

<system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="toolbox" />
        <add namespace="aa.App_Code"/>
      </namespaces>
    </pages>
</system.web.webPages.razor>

try adding here, modify view (add some tag), and check if it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜