+2  A: 

There is no concept of controls in ASP.NET MVC any longer.

You have two options:

  1. When the user clicks a button, you handle this POST request in a controller action, set some sort of flag in your view model to now show a textbox, then return the same view which in its turn will look at the flag and generate a text box if required. This will incur a complete round-trip which will be somewhat similar to the postback in WebForms.

  2. You do it in-place with JavaScript. You intercept a click event on a button and you inject an input/textarea HTML element into your document structure.

Developer Art
I think that it would be far easier in .NET MVC to create input areas like textboxes than webforms due to the problems with ids and controls.For that reason, I think the second solution is probably best.
Dan Atkinson
A: 

I created classes for all the controls i need and then just referenced that in my action in the controller and then i used a .display property in my class and showed all the controls . it was fun .. anybody need help post a comment i will post the code for that .

lucky
You should consider updating your question, rather than adding a new answer. If you feel that you have answered your own question, feel free to provide the solution you used for future users and also close the question.
Dan Atkinson
dude as if i know how to close a question ... and the code i am still rectifying it for my own use when is it ready i will post it ...
lucky
A: 

Hi lucky,

Am very new to mvc.I need to create dynamic controls in mvc for my project.Can u please help me with your code.

FYI: If you put spaces before your text, you will end up code indenting it. Also, as mentioned, there is no real concept of controls in ASP.NET MVC.
Dan Atkinson
A: 

Hi Lucky,

I have got few doubts in your code.It might be silly,eventhough please clear my doubts.you have created all those classes in controller's folder right?.And later your are referring it in the actionresult of your HomeController class right?.Now while creating a view for this ActionResult controller what is the type of the view content are you going to give for it say for example(create,edit,empty,details,list).And later on what is the display property.how our dynamic controls are going to be called in that particular aspx page.

all my classes are there in a separate folder classes and i created a strongly typed view with a reference to my class . remember to take out i enumerable from the the page tag there . u will have to write all the rest manually .
lucky
A: 

Ok,but while creating a strongly typed view what is the view content type you are specifying it wheather(Create,edit,details,list or empty). What i have done is that i had created a view with viewcontent type list and then removed the IEnumerable from that view page later on i used this piece of code ViewData.Model.Display in that aspx page,then i was getting the following error message.

object' does not contain a definition for 'Display' and no extension method 'Display' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

please post your aspx page i cant solve it like that ....
lucky
Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YOUR-PROJECT-NAME.FOLDER-NAME-WHERE-THE-CLASS-IS-STORED.CLASS-NAME>" this is how your first line should look like .... i think that should solve that ...if still the problem persists try cleaning the solution and rebuilding .... if still continues try deleting the view and create a new view with strongly typed view and reference to the class
lucky
Hi lucky many thanks ...my project is working fine and the controls are getting displayed dynamically.. Thanks a lot :)
no prob... even i faced few of those problems ....so i know how to solve them ....
lucky
thank you very much :)
A: 

Way are you creating View info in the Controller? Like: case ("TextBox"):
/// Buliding a textBox box
controlbuilder.AddControl(new TextBoxBuilder(control.Field, control.ControlName, value));
break;

Shouldn't the code be placed in the View?

Madmaximus
i think i am doing that in the view ie displaying ... that is the whole point of view all your logic in controller and only dispalying in a view
lucky
A: 

Lucky, thanks for the quick reply.  I have another question, in the controller how do I validate the controls? Like when the edit post is called. How would that function look?

Madmaximus
use j Query validation thats what i have done pretty easy and more efficient ....
lucky
A: 

When I post the changes of the dynamic control what will the Post ActionaResult method look like,what parameters would you pass it? and How would it know what control you have made a change to ?

A: 

Lucky- Please upload the code of all controls which you have created. If possible please also upload the entire solution file that help to understand your work better.

Leo