简体   繁体   中英

Dynamically add inputs to view for properties

Unfortunately I'm relatively new to MVC so what I am trying to do might be quite simple or not even possible in MVC.

I have a series of template classes in a library which can have template added, change or removed between versions. What I am trying to do is create a page that will allow for the user to select from a drop down list which template they wish to work on and then once they have selected the template be able to populate the properties of the template through text boxes, drop down lists, date selectors etc.

Whilst I could in theory create a View for each of the different templates, I would like to avoid that as if the templates can be added/changed/removed with newer versions of the library, I would like to avoid having to rewrite the Views each time that happens.

Does anyone have any suggestions on how I might be able to achieve this or know of existing references dicussing this?

If I've missed out any information which would be of use please let me know.

Thanks for any help/advice in advance.

Satal

Without writing the code, what you are looking for is a component that builds html based on what model/object it is passed to satisfy the population of that model via a web browser.

I dare say that this is possible!

An easy or easier route would be:

  1. Decorate the properties within your template models with attributes that would dictate what sort of input control they require.

  2. Also decorate the properties with an attribute that specifies the name of the attribute.

  3. Create a component that accepts a model/type and creates HTML to satisfy the input of that type. Of course this must be rendered within a form.

  4. On postback/submit to your action use the Request["{propertyName}"] to get the value of input fields to populate the properties.

  5. Your controller action will accept the name of the Template so that you know which model to create

System.ComponentModel.DataAnnotations is a good place to start before writing your own custom attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM