views:

62

answers:

3

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? 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).

A: 

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.

Wyatt Barnett
Mayby there is no reason to implement everything from begining and You may be right : extending HtmlHelper is a better way...I just wanted to know why "(this HtmlHelper htmlHelper)" is not needed (greyed) in View's code and my extensions are forced to pass (ClassA classA) in each method in View's code
Simon
I honestly can't make sense of your question--perhaps you should put something more specific up than "classA".
Wyatt Barnett
A: 

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

Necros
I know how to use it in View() there is no magic. <%@ Import Namespace="NamespaceOfClassA" %> resolves problems with visibility in views.The main question wasn't: "How extend HtmlHelper" its simple and well described in many sources, but the question was "How to write class like HtmlHelper" - i wanted to create separate class and wanted to know dependencies which let me to cope with existing controllers.But thx i'll check in MVC's sourcecode
Simon
Sorry, I got the wrong impression from your question. I that case you are probably looking for something like @Ahmad's answer, but it's going to be a lot of work. It would be easier to just use jQuery and inject some stuff using Html/Url helper.
Necros
A: 

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.

Ahmad

related questions