R – Dealing with ASP.Net Custom Controls in MVC

asp.netasp.net-mvccustom-server-controls

I have looked around on the Internet trying to answer this question. It seems to be kind of complicated though! Seems to come up with all sorts of different topics such as the HtmlHelper and other stuff.

Basically. How can you use custom controls with MVC? In a way that avoids doing anything that is frowned upon.

Also, Is it true that with MVC you have to put some simple logic IN THE PAGE? Isn't that what we should be getting away from?

Update: So, Seems you cannot use controls which depend on anything which MVC does away with, such as the postback events and viewstate. Can you make your own then? A control which does only rendering of course.

Best Answer

Can you use ASP.NET controls? Maybe. If the control does not use ViewState or postback events, it will probably work. For example, the LoginView control works just fine. Telerik supports ASP.NET MVC with some of their controls. On the other hand, if the control needs either ViewState or postbacks, then it just isn't going to work. Controls with data binding aren't ideal, since you typically have to use code behind him up. But they will work, if you add code behind.

On the other hand, controls with no knowledge of ASP.NET (such as the jQuery UI controls) work very well.

No, you don't have to put your logic in the page. You just don't put it on a code behind page, either. Instead, use helper methods.