2

Well, I am creating this app that allows me to query certain DataBase using ASP.NET MVC 5 with Entity Framework 6.1.3.

Well I came across this situation. I have a DataBase with several connection strings that let me access different databases.

So I want to create a model where the user can create queries upon those databases. So I came up with idea to let the user create it as he/she were creating a question. For instance: Someone would want to know How many students are studying in the faculty {PLACEHOLDER}? where "How many students are studying in the faculty is a LABEL and {PLACEHOLDER} is a COMBOBOX. The ComboBox will be filled with a list of faculties (A query to a table of a database).

My idea is provide the user with links in order to add as many Labels, TextBoxes, ComboBoxes, etc. for him to create the question. If the user were to create the question above he will add a Label, and then a ComboBox. Then he/she will fill the information, and with some other function (i am to develop but not the main goal of this question) will satisfy the query.

So, how can I runtime create a link that add me those (label, textbox, etc.) items in runtime to the view, and then gather that information and save it into the database (for the model in question)?

I´m guessing my link from the view(or viewmodel?) has to pass some information to the controller and then do something and render the view back and let the view know that there is a new "control" (how I named the model for those controls) and render it (or else). How can I achieve that?

Hope I explained myself. If not, please comment and I´ll try to do it better.

Thanks in advance!

DarK_FirefoX
  • 686
  • 6
  • 15

1 Answers1

1

You're trying to create dynamic fields, right?

You can add the inputs and selects with JavaScript,then, in your controllers, you can parse as many objects parameters your need.

For example, you can create a new model called "Question", with a defined structure like:

    public class Question {
       public string Label {get; set;}
       public string PlaceHolder {get; set;}
    }

And in your controller:

    public ActionResult SomeController(Question[] questions)
    {
     ....

The most difficult part is creating the submit data.

I found these links in the SO that can help you with that.

Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

MVC Form not able to post List of objects

How to pass array of objects to an MVC Controller in Jquery?

Community
  • 1
  • 1
Victor H
  • 181
  • 1
  • 7