How to dynamically generate controls in MVC?

mvc dynamically add form fields
creating dynamic forms with mvc and jquery
adding textbox dynamically and save values to the database in asp.net mvc
how to create dynamic page in mvc
mvc dynamic model binding
page controls add in mvc
dynamic controller in mvc
spring mvc dynamically adding form elements

In asp.net, you can use asp:Repeater for dynamically generating controls.

for example:

<asp:Repeater runat="server" id="rpt">
   <ItemTemplate>
       <asp:Textbox runat="server"/>
   </ItemTemplate>
</asp:Repeater>

Then you can get loop through the items on the server side:

foreach(var items in rpt.Items)
{
}

And in MVC, you can do something like this:

@foreach(var item in model)
{
   @Html.TextBoxFor(a=>item.searchCriteria);
}

But there is a problem:

I would like to generate textboxes when ever user clicks add more textbox button:

If I use foreach in MVC, then I won't be able to fetch the user input on the controller, also, I don't have a model to loop through.

And the final desired output will be:

After the user clicks the "Add more textbox" button, a new textbox will be append to the bottom, and after the user clicks the search button, the text inside the textbox will be generated into a query string, something like this:

View:

 <input name="searchCriteria" type="text" class="form-control" />
 <input name="searchCriteria" type="text" class="form-control" />
 <input name="searchCriteria" type="text" class="form-control" />
 <input name="searchCriteria" type="text" class="form-control" />

Controller:

 public ActionResult Search()
        {
            string queryString = Request.RawUrl;
            //parse the querystring here
            return View(NumberOfVisitorsReport);
        }

And the query string generated will be:

www.website.com/?searchCriteria=a&searchCriteria=b&searchCriteria=c

So are there any alternative way to achieve this in MVC?

Edit:

I've took a look at some post on "How to generate controls dynamically in MVC"

For example

Control creation based on model value in MVC4

He is generating controls based on value in database, if I implement this to my website, the controls will lose value on each post back.

You must use jquery.

This article can help for solve to problem.

Dynamic Form in Asp.net MVC & jquery

How to add control dynamically in Asp.net MVC using Javascript , Here I will explain How to add control dynamically in Asp.net MVC using JavaScript. We can also remove generated control on button click. I hope you are new to MVC. If you have a view model in the cshtml file, just use the view model and loop the same way to generate the controls dynamically. Ex: If your view model is Demo.ViewModels.EmployeeData foreach (Demo.ViewModels.EmployeeData emp in Model.EmployeeViewList) { < div > < @Html.TextBoxFor(model = > emp.EmpName) < @Html.TextBoxFor(model = > emp.Designation) < /div > }

You probably want to move away from "postback" and look to adding text boxes dynamically in the browser using jQuery or a similar framework. If you either name the new text boxes carefully or do the submit as an AJAX post of a JSON object, you can harvest all the values from all the added controls.

How to add dynamic controls to the mvc view, In this article, I will attempt to walk you through the process of creating MVC form controls dynamically, in this instance with an onclick event,  Follow these steps in order to implement “Add control dynamically in Asp.net MVC using Javascript in Asp.net MVC” Step1: Create New Project. Go to File > New > Project > Web > Asp.net MVC web project > Enter Application Name > Select your project location > click to add button > It will show new dialog window for select template > here we will select empty project > then click to ok . Step2: Create a controller.

You are going to have to use For loop insead of ForEach. Then use the index to Create new Textbox controls, this way when you submit the form, index of newly created textbox will be submited, you can even BIND it to a model if the data type of your model is LIST.

Example:

 @for (int i = 0; i < Model.Contributor.Count(); i++)
    {

        @Html.HiddenFor(model => Model.Contributor[i].Id)

  }

now you know the value/index of "i", use that to populate new controls.

How to Add MVC Form Fields Dynamically, How to create Controls dynamically in MVC 5 I have to perform task that i have to create one admin page where site admin can add/update  So my aim is to read the XML file and based on the control type, I need to create that control dynamically and associate the initial value if any and the preview of the page have to be shown in a view. Anybody please help me how to create the controls dynamically in MVC 3 for this scenario. For eg: my xml file will look something like this.

How to add controls dynamically in asp.net mvc using javascript , The dynamic TextBox controls with Add Remove Button functionality will be created on Client Side using jQuery in ASP.Net MVC Razor. I am using c# MVC 4 and I would like to create user controls and fill data dynamically. Eg: I want to send data like(So I do not know the field names and its control types prior to viditing this pa

Dynamically add or remove control in ASP.NET MVC, In the following code from my MVC 5 View, I am dynamically building label and code which generate label and textbox controls dynamically. So the set of controls where user enters details needs to be dynamically added when user says "Add another". I googled bit but couldn't get much apart from creating controls using jquery. My main problem is how to render controls dynamically and then get their values while submitting the form.

create controls dynamically in MVC 5, Hello Everyone, I have to create one view in that view control will add dynamically on button click. there will around 8-10 control will add on  I couldn't manage to find an example to create buttons on page load. Most of the "dynamic buttons" examples I came across are triggered by a button click. I suppose I'm going to read the data in a [HttpGet] in the controller.There I can get the necessary row count and data, but I have no idea about creating the buttons from that point.

Comments
  • You can use JavaScript...that's generally the best approach when you want to make something happen on the client side.