I'm trying to create an htmlhelper extension in MVC4, but I'm having problem when trying to call it. Every samples I've found on the web are similar to what I'm using but somehow, while they all have the "this HtmlHelper html" parameter,
none require to call it in the .cshtml page but for some reason, it's expecting in mine!!
Here is my Extension class:
public static class HtmlHelperExtensions
{
public static string Image(this HtmlHelper helper, string name, string url)
{
return Image(helper, name, url, null);
}
public static string Image(this HtmlHelper helper, string name, string url, object htmlAttributes)
{
var tagBuilder = new TagBuilder("img");
tagBuilder.GenerateId(name);
tagBuilder.Attributes["src"] = new UrlHelper(helper.ViewContext.RequestContext).Content(url);
tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return tagBuilder.ToString();
}
}
But it's giving the following error when I run my app:
The best overloaded method match for 'WebTestUpload.HtmlHelperExtensions.Image(System.Web.Mvc.HtmlHelper, string, string, object)' has some invalid arguments
This suggest that you are defining an extension for the specified class (HtmlHelper in this case), and you can call that method like a static method of that class.
I tried what you suggested, but no luck. I get this error now:
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper<dynamic>' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<dynamic>' could be found (are you missing a using
directive or an assembly reference?)
Bang on!! Thank you for that! Messy from MS if you ask me, well at the very least not obvious!
One small thing I'll add here in case some one else reads this thread, but performing the above will solve your problem but you may end up with the following exception being thrown:
CS1973: 'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'Image' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension
method without the extension method syntax.
This is caused (again, strange behaviour or outcome I think!) because one of my model's properties needs to be cast as a string
tfierens
Member
6 Points
30 Posts
HtmlHelper Extension MVC4
Jan 17, 2013 01:53 AM|LINK
Hi,
I'm trying to create an htmlhelper extension in MVC4, but I'm having problem when trying to call it. Every samples I've found on the web are similar to what I'm using but somehow, while they all have the "this HtmlHelper html" parameter, none require to call it in the .cshtml page but for some reason, it's expecting in mine!!
Here is my Extension class:
public static class HtmlHelperExtensions { public static string Image(this HtmlHelper helper, string name, string url) { return Image(helper, name, url, null); } public static string Image(this HtmlHelper helper, string name, string url, object htmlAttributes) { var tagBuilder = new TagBuilder("img"); tagBuilder.GenerateId(name); tagBuilder.Attributes["src"] = new UrlHelper(helper.ViewContext.RequestContext).Content(url); tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return tagBuilder.ToString(); } }and here is the my code in my .cshtml file:
@if (Model != null) { foreach (var item in Model) { <tr> <td> @item.Size </td> <td> (@item.Size / 1000) KB </td> <td> @WebTestUpload.HtmlHelperExtensions.Image("previewImage", item.Name, item.Path, new { width = 100, height = 100 }) @*<img src='Url.Content(@item.Path)' alt='item.Name' />*@ </td> </tr> } }But it's giving the following error when I run my app:
The best overloaded method match for 'WebTestUpload.HtmlHelperExtensions.Image(System.Web.Mvc.HtmlHelper, string, string, object)' has some invalid arguments
Any ideas?
Thanks.
CPrakash82
All-Star
18290 Points
2844 Posts
Re: HtmlHelper Extension MVC4
Jan 17, 2013 03:03 AM|LINK
This suggest that you are defining an extension for the specified class (HtmlHelper in this case), and you can call that method like a static method of that class.
You need to change the call like this.
@Html.Image("previewImage", item.Name, item.Path, new { width = 100, height = 100 })
tfierens
Member
6 Points
30 Posts
Re: HtmlHelper Extension MVC4
Jan 17, 2013 01:29 PM|LINK
I tried what you suggested, but no luck. I get this error now:
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper<dynamic>' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<dynamic>' could be found (are you missing a using directive or an assembly reference?)
Any other suggestions?
CPrakash82
All-Star
18290 Points
2844 Posts
Re: HtmlHelper Extension MVC4
Jan 17, 2013 01:37 PM|LINK
You need to use the namespace before you call this method. i.e using statement in the start of the file
@using WebTestUpload
tfierens
Member
6 Points
30 Posts
Re: HtmlHelper Extension MVC4
Jan 17, 2013 05:32 PM|LINK
Bang on!! Thank you for that! Messy from MS if you ask me, well at the very least not obvious!
One small thing I'll add here in case some one else reads this thread, but performing the above will solve your problem but you may end up with the following exception being thrown:
CS1973: 'System.Web.Mvc.HtmlHelper<dynamic>' has no applicable method named 'Image' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
This is caused (again, strange behaviour or outcome I think!) because one of my model's properties needs to be cast as a string
@Html.Image("previewImage", (string)item.Path, new { width = 100, height = 100 })Thanks again!