HtmlHelpers are really useful, also i was using AjaxHelpers untill i wanted to do something they can't... so now i'm prefering jquery+javascript, but i don't like to write in javascript (mayby because i never know it as good as i wanted) and to make life easier i wanted to implement something like JQueryHelper, but right now i don't know how
I wanted to use inside of "Helper" resolved URL's, because most methods would just pass data to controllers etc... and update some parts of UI, so i'd like to create something like AjaxHelper, but based on JQuery, not MSScripts.
What should I include in such class?开发者_StackOverflow社区 how can i get context, url's and generate proper javascript code which can by dynamicly injected into ex.
<div onclick=JQuery.Action("<ActionName>", "<Controller>", new { param1, param2}, <Id to Update>)> My Div</div>
How is it implemented in HtmlHelper or AjaxHelper?
Edit: I've tried few simple implementation, but i think I'm lack of knowledge and don't know how to implement class with extensions exacly.
I've ClassA(class) and ClassAExt(extensions): I've something like that:
static public class ClassAExt{
public static MvcHtmlString Method(this ClassA classA) {
}
}
But in View(), when I'm using ClassAExt.Method() i have to pass also instance of ClassA (in helpers Ajax and Html this argument is grey (optional? not needed?), how to get such effect).
I am not sure if I understand your question correctly.
The HtmlHelper also get instantiated (i.e. new HtmlHelper()
) during the course of page rendering and user control rendering. Ajax
and URL
helpers also get instantiated and this is what give one access to the various variables such HttpContext etc.
So in order for you to use your helper class related to ClassA
you too will need to instantiate it. I think then the question leads to how do I do this? You will probably need to extend the ViewPage
, ViewUserControl
and ViewMasterPage classes.
Its non obvious because its so obvious. Basically, you extend html helper's extension:
public static HtmlString Foo(this HtmlHelper helper, string param)
{
//do whatever
}
You can get at request context, etc, from HtmlHelper.
Way I see it, you problem is that you don't understand extension methods.
if you have
static public class ClassAExt{
public static MvcHtmlString Method(this ClassA classA) {
}
}
you use it in view like this:
<%: ClassAInstance.Method() %>
to do that, you have to add <add namespace="NamespaceOfClassA" />
to you web.config's pages/namespaces section.
This way, you can extend the HtmlHelper
class to do whatever you need, so there is really nothing it can't do. If you want it to do something it can't by default, just write an extension method. Also download MVC Futures assembly, there's a dll Microsoft.Web.Mvc
that contains (among other things) a lot of useful extension methods for HtmlHelper
.
If you want to know how they implemented HtmlHelper
or AjaxHelper
you can check out MVC source code.
Link to download both MVC Futures and MVC source code: http://aspnet.codeplex.com/releases/view/41742
精彩评论