Steoates
Steoates

Reputation: 3168

Creating asp.net MVC custom controls

I'm currently in the process of migrating one of our WPF applications to MVC as a proof of concept - one thing we have a lot of is custom controls..

What I'm hoping you guys can help me with is some resources on how to get started and how exactly they should be structured for advanced things.

I've looked around so far and it seems you create use extension methods to render your HTML - which is all fine.

here is my first scenario - any advice, tips, resources and help would be brilliant!

Search Provider: this isn't just an autocomplete box, it has advanced options that trigger modal views etc .. what I need is to be able to bind this to an object on the view model..something like this

        @Html.SearchProviderFor(model => model.NameObject, "NameSearch");

hopefully - this would update the modal with the complex object the search provider will return.

what exactly is the best way to "For" extensions? and how is the best way to approach creating custom controls that are just not "display this like that" :) ?

I'm sorry if the question is abit fuzzy but I'm hoping you guys can make it out!

cheers. ste.

Upvotes: 0

Views: 800

Answers (1)

Steen Tøttrup
Steen Tøttrup

Reputation: 3835

Extension method are nice, but if we're talking about a lot of html/css/js work, you should probably be looking at custom type editors instead, here I'm talking:

@Html.EditorFor(m => m.NameObject)

Then you create a EditorTemplates folder and create a view with the same name as the type of the NameObject type.

Take a look here: https://stackoverflow.com/a/5497234

The "For" indicates that the extension is working on a property and not just a value. Here's an example of a extension method using for:

public static String IdFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { ... }

Upvotes: 1

Related Questions