mvc 3 Ajax.ActionLink 图像不起作用
我正在使用 vs2010 win7 x64 MVC 3 和常规 asp.net 开发服务器。以及最新版本的 Firefox、IE9 和 Chrome。
我有一个强类型视图,我试图通过此代码的稍微修改版本来获取 ajax 扩展方法 asp-net-mvc-ajax-actionlink-with-image Arjan Einbu 为 mvc 3 提供的。
这是我的扩展方法代码,位于 Helper.cs 文件中我的 mvc 网站的 App_Code 文件夹。
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)));
}
}
在我看来,我在定义模型的顶部有 @model IEnumerable
,我正在使用 ...
@Ajax.ImageActionLink(Url.Content("~/Content/ThumbUp.png"), "Valid/Appropriate", "VoteUp", new {id = item.ID}, new AjaxOptions { HttpMethod = "POST" } );
插入带有图像的 ajax 操作链接,以允许用户投票该项目。上面的代码不起作用,无论我做什么,它都会在无尽的错误之后不断地给我错误。它给我的当前错误是......
编译器错误消息:CS0121: 之间的调用是不明确的 以下方法或属性: 'HelperExtensions.ImageActionLink(System.Web.Mvc.AjaxHelper, 字符串,字符串,字符串,对象, System.Web.Mvc.Ajax.AjaxOptions)' 和 'HelperExtensions.ImageActionLink(System.Web.Mvc.AjaxHelper, 字符串,字符串,字符串,对象, System.Web.Mvc.Ajax.AjaxOptions)'
这意味着我在同一命名空间中有两个具有相同名称和签名的方法,它们是冲突的,这也是不可能的,因为我只在一个代码文件中定义了它一次。所以我认为可能存在缓存问题,所以我在 Firefox 中执行了 ctrl+shift+delete 操作,并完全清除了缓存、历史记录和其他所有内容。刷新页面,我得到同样的错误。我也在 IE9 和 chrome 中测试了同样的问题。
我停止并启动了 asp.net 开发服务器,退出 Visual Studio 并重新启动,仍然存在同样的问题。
磁盘清理是否检查了所有内容并运行了它。磁盘清理没有删除“C:\Users\Dean\AppData\Local\Temp”中的所有文件,因此我手动删除了该文件夹中的所有剩余文件,包括“Temporary ASP.NET Files”文件夹。然后我又运行vs2010,运行网站。仍然是同样的错误“CS0121:以下方法之间的调用不明确......等等”
无论我做什么,我都无法摆脱这个错误。这不是我遇到的唯一错误。在这个错误开始持续存在之前,我还收到了与 mvc 无法找到具有正确签名的 ImageActionLink 方法相关的错误,即
ImageActionLink(this AjaxHelper<IEnumerable<SpeciesType>> helper, etc etc
我认为该错误与我使用强类型视图这一事实有关。
因此,为了尝试消除“调用不明确”错误,我注释掉了整个 ImageActionLink 方法并重新编译并再次运行该网站。我收到这个错误
编译器错误消息:CS1061: 'System.Web.Mvc.AjaxHelper>' 不包含以下定义 “ImageActionLink”且无扩展名 方法“ImageActionLink”接受 类型的第一个参数 'System.Web.Mvc.AjaxHelper>' 可以找到(您是否缺少 使用指令或程序集 参考?)
该错误完全有道理,因为 ImageActionLink 不再存在。所以接下来我做的就是取消 ImageActionLink 方法的注释,重新编译并再次运行网站。果然同样的“调用不明确”错误。
我不知所措。我已经做了我能想到的一切来清除各种缓存以排除这种情况,注释和取消注释代码片段并测试哪些部分损坏。但我无法通过“调用不明确”错误。
任何人都知道如何修复这个“调用不明确”错误,这样我就可以继续问为什么我似乎无法让一个简单的 ajax 扩展方法工作?
我摆脱了“调用不明确”错误。发生的事情是我复制了在这里找到的 Arjan Einbu 的 mvc 3 版本的代码 asp-net -mvc-ajax-actionlink-with-image。
然后,我在 Visual Studio 的 App_Code 文件夹中创建了一个新类,并执行了 ctrl+a ctrl+v 操作,并将所有预先生成的类模板代码替换为 HelperExtensions 类。问题是我没有使用与网站命名空间匹配的命名空间来包装 HelperExtensions。
有关 asp.net 如何在没有名称空间的情况下编译代码文件的问题将其搞砸了。我假设这可能是因为代码是在构建时在主网站 dll 中编译的,然后当 asp.net 运行时,它会编译在 app_code 文件夹中找到的任何代码文件,因此两个类具有相同的名称和相同的方法名称或某些这样的。
无论如何,现在我已经解决了这个问题,我运行我的网站,并收到以下错误...
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?)
该错误看起来好像正在查找 ImageActionLink 方法,但 ImageActionLink 方法上的第一个参数不是 AjaxHelperAjaxHelper
AjaxHelper
AjaxHelper
AjaxHelper< IEnumerable
该错误不太有意义。是的,ImageActionLink 方法的第一个参数是 AjaxHelper 类型,但不是通用 AjaxHelper 类型。所以我尝试像这样修改 ImageActionLink 方法声明
public static IHtmlString ImageActionLink(this AjaxHelper<IEnumerable<SpeciesType>> helper, string imageUrl, string titleText, string actionName, object routeValues, AjaxOptions ajaxOptions)
但我仍然收到相同的错误。 ImageActionLink 上的第一个参数是 AjaxHelper 类型,但它被定义为“this AjaxHelper helper”,这意味着它是一个扩展方法,应该扩展 AjaxHelper 类。
我实际上并不需要 index.cshtml 文件顶部的 @model IEnumerable
因此我将其删除并尝试再次运行该应用程序,但这次出现相同的错误
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?)
有人知道为什么我会收到此错误吗?以及如何修复它?
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 properties:
'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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 Web.config 中的何处添加了命名空间(您正在使用剃刀视图)?到页面或
尝试在此处添加,修改视图(添加一些标签),并检查它是否有效。
Where in Web.config have you added namespace (you are using razor views) ? To pages or to
try adding here, modify view (add some tag), and check if it works.