1

I need to port the functionality of this one and only one AJAX control to MVC, but given the poor selection of MVC controls out there, I think I need to bring this legacy control into the MVC world...

I'd rather not taint my MVC project with ASP.NET controls, and welcome json/MVC alternatives you know of. (post them if you know of any)

Sample UI that I need in ASP.NET MVC Combo sample

Back to porting

Although it's unfortunate that I am left to porting this control to MVC, it seems to be a widely accepted practice since Telerik has detailed instructions on how do this.

That makes me ask:

How common is it for a MVC website to use ASP.NET controls?

Again I'll mention I don't want to do this so I welcome MVC-specific alternatives. That being said, I'll proceed with trying to merge that control with my existing site. </End Disclaimer>

If you click on this hyperlink, and look at the source code at the bottom, can you tell me where I should put the following in MVC?

  1. Code behind (My first instinct is to use a Controller but another SO question indicates I should create a create a ViewName.aspx.cs file)

  2. How do I port the SQLDataSource to the new "Model" way of thinking. I know they are different in nature but I don't know how to present data to a ASP.NET control in a way that it will consume the information.

  3. How do I handle the AJAX component? This control has an AJAX component using callbacks. Yes this is getting ugly, but it seems like I have to do this.

  4. Apparently this model saves data in session or view-state. I have no idea if this even work in MVC. Guidance, an alternate control, or a life preserver is much appreciated.

I've already done research and have instructions from Telerik here and here that describes how to get started with placing a simple menu, but I need a little assistance with the more complex controls like this one.

Note: For all the commentary that has hit this question, please remember that I only want this one ASP.NET control functionality; I can't find a comparable control in MVC.

Community
  • 1
  • 1
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • Why so many down votes? Is it such a bad idea to reuse one and only ASP.NET usercontrol within MVC? I'll appreciate any constructive feedback, but so far this isn't more constructive than what I'm doing with www.asp.net/learn – makerofthings7 Oct 25 '11 at 14:48
  • You are trying to port into ASP.NET MVC something which is not an MVC farmework standard (using SQLDatasource to bind a control) – humblelistener Oct 26 '11 at 13:24
  • @KeYan I understand that the SQLDatasource isn't MVC but what I don't understand is if it's even possible to port this type of control to MVC. – makerofthings7 Oct 26 '11 at 13:27
  • You are probably going to have to create something yourself to replicate the control - I doubt that you will be able to 'port' it. You could probably use the [JQuery UI AutoComplete plugin](http://jqueryui.com/demos/autocomplete/#custom-data) to achieve this. – StanK Nov 02 '11 at 03:49

6 Answers6

6

porting from asp.net webforms to MVC is a paradigm shift.

Directly porting does not work.

The Model is where you typically describe your data and do the data access
the View is for displaying the data
The controller plums the other two together

So SQLDataSource is your data access layer and would therefore go to your model the problem with the thought pattern of SQLDataSource == Model then you get away from the point of decoupling your presentation from data access

You have to think of MVC development as a new build

I would pick a book or video series from your preferred source and learn starting with MVC3 (it has some differences that simplify build speed and reinforce the difference between webforms and mvc)

MarkKGreenway
  • 8,494
  • 5
  • 34
  • 53
  • I somewhat understand the mental shift, but have a legacy ASP.NET component I need in my MVC site, and there is no replacement. I'm trying to make this old SQLDataSource component work with MVC. How do I tie in something that wants a SQLDatasource into the "model" way of thinking? – makerofthings7 Oct 21 '11 at 17:13
  • 4
    Hurricanepkt is correct, it's apples vs. oranges. You should probably re-evaluate the reason for porting from web forms to MVC. If the end goal is to make a 1:1 conversion then you're missing the goal of MVC. – Ed Charbeneau Oct 21 '11 at 18:38
  • @EdCharbeneau My goal has never been to make a 1:1 conversion. I just need to port one usercontrol that has to appear on every page (via the master). Is it impossible to take a MVC Model and fake it into a SQLDatasource? – makerofthings7 Oct 25 '11 at 05:20
  • you can create a model which is an List where my class is the item that would appear in your datasource. Example: datasource.item is equivalent to myclass. – Ed Charbeneau Oct 26 '11 at 01:08
1

Add an IFrame in your MVC view that just shows the WebForms page (or just use that control on a single WebForms page).

There is nothing that says you can't have a site with both WebForms and MVC pages. You can route a single URL to a WebForm just for this control.

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
  • 1
    If you have a constraint on your project that says you MUST use this WebForms control, it begs the question of why you are doing any of it in MVC? Do it in WebForms. As stated in the other answer, forcing the WebForms control in to the MVC model is not a good idea and significantly removes much of the benefits of MVC's separation of concerns. Of course Telerik will tell you how to do it, because their goal is to market their control for as wide an audience as possible, that doesn't mean you SHOULD use it... – Paul Tyng Oct 27 '11 at 15:33
  • For @Paul T. Quote from my question: `I can't find similar functionality in an MVC control, though I welcome alternatives`. Again, constructive feedback is welcome. What you may not know is that the MVC site is already complete, and this requirement was added last minute. Lastly this type of comment is best located at the top, beneath the question, not within an answer below it. – makerofthings7 Oct 27 '11 at 16:01
  • The option of just doing a single WebForm page may not be evident to others having this problem which is why I wanted to call it out as a possible solution. It may not solve your specific issue, as you seem to have painted yourself in to a corner architecturally but that doesn't mean the answer won't have use for future readers. – Paul Tyng Oct 27 '11 at 16:16
  • Oops, I thought your comment and the answer were two different people. I appreciate the suggestion! – makerofthings7 Oct 27 '11 at 16:25
1

I can't find similar functionality in an MVC control

MVC doesn't really have a concept of controls in the same way that ASP.Net does - there are only really the plain old HTML controls (i.e. hidden input, text input, checkbox, radiobuttons, select box, text area, password and buttons).

When you need something more complicated than the plain HTML Controls you need to use some JavaScript to achieve this.

I'm not sure that you will be able to 'port' the control into MVC - you will most likely have to try and re-create it your self using an MVC controller and a partial view with a fair bit of a javascript to create the control.

Have a look at the JQuery UI Autosomplete plugin - you could probably use this to acheive something similar

StanK
  • 4,750
  • 2
  • 22
  • 46
1

Hope this helps.

Michael Grassman
  • 1,935
  • 13
  • 21
1

Why not just use the telerik MVC controls? They work quite well. Either get them via a NuGet package or visit this link http://www.telerik.com/products/aspnet-mvc.aspx

Carlo Kuip
  • 311
  • 2
  • 3
  • Thanks Carlo, I wasn't able to find a replacement for the multi-column dropdown combo that allowed searching as they type – makerofthings7 Nov 01 '11 at 15:28
  • They even have a sample of this. Pretty RAD i'd say http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multicolumncombo/defaultcs.aspx – Carlo Kuip Nov 01 '11 at 16:24
  • That is the exact Ajax control I am tying to port to MVC (also the one I linked in my question). Not sure how to make it work in MVC – makerofthings7 Nov 01 '11 at 16:30
  • 1
    @makerofthings7 Started looking further into your efforts. It's a world appart indeed, the filtering highlight hits and reduces the list though but the layout doe not allow multiple columns. Pretty strange for templated controls to have this limitation. – Carlo Kuip Nov 01 '11 at 18:42
  • If you know of a way to do something similar in ASP.NET MVC, perhaps with Jquery, that would be fantastic. I've seen many similar controls, but nothing supporting multiple columns on dropdown – makerofthings7 Nov 01 '11 at 22:25
1
  1. I would rather use ViewModel instead of code behind

  2. You don't have to throw away SqlDataSource you can use result set and buld from it your model, problem may be column names in result set... tricky but can be done

  3. Since there is no components in MVC except helpers youll need help of jQuery probably, it easy

    $.ajax({ url : "/controller/action", data: { /*json or serialized form */ }, successs: function(data){ //if you got response as html from /controller/action $("#some_div").html(data); } }

  4. Session is available in MVC but viewstate not, you can use HttpContenxt.Cache or TempData if you need something like viewstate. USe TempData to keep data between redirections, or httpcontext.cache to cache your data further more.

Milan Jaric
  • 5,556
  • 2
  • 26
  • 34