3

I'm strugling on this for quite a while now. I need to create a custom mvc3 html helper for easy filter and toolbar management.

All that helper should look something like this below or something similar. What i want to do is to have a helper where i could give toolbar buttons and filter fields and it generate a toolbar and filter form for me.

I just can't figure it out, because i'm quite new in asp.net mvc

@Html.Toolbar(x => {
   x.AddFilterButton();
   x.AddButton("Naujas");
   x.AddDropDownList();
   },
   @<text>
   @Html.EditorFor(Model.ListFilter.Name)
   Filter
   ResetFilter
   </text>,
   filterVisible: false)

How i could achieve this? Any help would be highly apreciated. Thanks indeed.

1
  • 1
    I would like to help you. But there are many different things mixed in your question, so i need a hour to answer ;) For your lambda (x => { x.Add(...)) part you should get familiar with expression trees. The rest is not hard to master. I suggest you to download the source of mvc3, mvcContrib or telerik. They had this in their grids and other controls. Commented Nov 14, 2011 at 16:53

1 Answer 1

1

Something like this:

public static class ToolbarExtensions {
    public static Toolbar Toolbar<T>(this HtmlHelper<T> html) {
        return new Toolbar();
    }
}

public class Toolbar : IHtmlString {
    public string ToHtmlString() { /* build your HTML off the state here */ }

    public Toolbar AddButton(string label) {
        /* build up state here */

        return this;
    }
}

The syntax on this would be a little different, instead of a lambda, would look like this:

@Html.Toolbar().AddButton("Button 1").AddButton("Button 2")

But you could easily change it to use a chaining object in the lambda instead of on the Toolbar method call.

The IHtmlString interface tells the ViewEngine to output the object as raw HTML when its encountered. The chaining is just achieved by returning the current instance in your methods after modifying the object state.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.