Form Fields generated based on variable types

I have a project utilizing ASP.NET MVC and Razor page layouts. The page in question will be a survey whose questions, datatypes, and answers have been configured by an admin user and retrieved from a database. For example:

public class ExampleViewModel { //the user define dquestion public string Question1Text { get; set; } //this is an enum with "Text","YesNo","DropDown" public AnswerType Question1Type { get; set; } //this would hold options for the drop down list public string Question1Options { get; set; } //the user input answer public string Question1Answer { get; set; } }

What I am not sure is how to structure the Razor view to create the appropriate type of form input field depending on the AnswerType. I seem to recall something about creating templates for the various DataType() annotations but I am not sure where to start looking at that and if that applies in this case?

-------------Problems Reply------------

You want to use Templated Helpers - Here is a good walkthrough -

In the helper itself you can do stuff like:

@if (model.AnswerType is xxx)
<button> xxx </button> - or your html

etc Views:1 Time:2018-09-06

Related post

  • Javascript to get sharepoint form field value to a variable 2011-12-19

    I want to copy the content of a sharepoint form field to a variable using Javascript. Eg. I have a field named "Language" in my sharepoint edit form. Now I just want to get the value of that field to a varaible x. Please help. BR --------------Soluti

  • Compare a form field with a String variable 2014-03-19

    String password = (String)session.getAttribute("password"); <input type = "password" placeholder = "Type your current password" id = "pass" /> Now, I want to compare the value in the form field with the variable "password".How can I do so by us

  • Form fields generated by Spring Roo 2012-02-16

    Does anyone know how to customize the form fields that Spring Roo generates so I can change the with of the associated label? For example, <field:input field="firstName" id="c_com_myclass_Person_firstname" label="What is your first name?" z=""/

  • JavaScript check field value based on variable value 2010-04-21

    I have an anchor like this: <a href="#" rel="1 4 7 18 ">Anchor</a> Where 'rel' attribute values are ids of some items. Than I have a form with an input, where user should type an id and click submit button. On submit button click I need t

  • How can I use customer error messages on form fields generated by Ruby on Rails when using the jQuery validation plugin? 2010-11-05

    I'm trying to use jQuery Validation plugin with customer error messages in a form on my site. The way to specify customer messages is as follows: $('#user_new').validate({ messages: { user_email: { required: "dd ", email: "Please enter a valid email

  • How to display a validation error message inline with form field on WordPress Custom Post Type? 2011-05-30

    I've created a Custom Post Type with a Custom Meta Box for WordPress. I want to display an error message if the user submits data into one of the form <input> fields is invalid. From what I understand add_action('admin_notices', '...') can be u

  • Naming standards for web development, eg. form fields, css class, javascript variables 2010-11-17

    I work for a company which has multiple websites, all of sites somehow share some kind of common code. Recently I am working on a project to revamp the UI of a particular website, here are some the problems I have faced: I started to "web parts" conc

  • SQL - field concatenation, based on variable 2010-10-14

    I have a need to build a string from Last Name, First Name, Middle Initial according to the following rules: If the Last Name is unique, just return the Last Name If the Last Name isn't unique, but the first letter of the First Name is unique, return

  • Conditionally render form fields based on user selection 2012-01-18

    The application has several types of properties, such as house, apartment, shop, land, farm, etc.. First, the User selects the type of property in a select field and based on the type of property selected, i need to render the rest of the form with o

  • jQuery - how to form field type? 2011-09-15

    I have a situation where I need to extract a value from a form field, but I'm not sure what type it is (<input /> or <select />). Two questions: Is there a simple way to detect what type of input this is (name of tag or something)? Is the

  • Using PHP to posting mutiple HTML form fields 2011-12-26

    I am making an ordering form and all of the products' data are stored in a MySQL database. There is a menu page with 10 items, each item has its own drop-down list for quantity (qty). I am using PHP to generate HTML form elements (eg. input textfield

  • Multiple form fields with same 'name' attribute not posting 2012-01-31

    I'm dealing with some legacy HTML/JavaScript. Some of which I have control over, some of which is generated from a place over which I have no control. There is a dynamically generated form with hidden fields. The form itself is generated via a Veloci

  • Converting a multi-dimensional array into HTML form fields - how? 2011-02-09

    I'm trying to take a multidimensional array and convert it into HTML form fields, like this: <input type="hidden" name="c_record[contact][0][name]" value="First Last"> <input type="hidden" name="c_record[contact][0][date_submitted][date]" va

  • Get Django form field from model field 2010-04-06

    I'd like to create a form that includes fields from two separate models, along with some other regular (non-model) fields. The form will create an instance of each model. I don't think I can use inline formsets for this, since I don't want to include

  • json2 from multiple form fields, getting bad values in jQuery 2010-10-19

    I have a setup with multiple form fields.. <input type='text' id='Trait1' >0</input> <input type='text' id='Trait2' >1</input> <input type='text' id='Trait3' >2</input> <input type='text' id='Trait4' >3</i

  • iterate in form fields using jquery 2011-08-26

    suppose i have form and it has many form fields. how can i iterate with in all form fields and check the input type and collect the value my form fields are <form id="form1" runat="server"> <div> <fieldset> <ol> <li>

  • update form field with js but not showing in submitted data 2012-02-10

    Experimenting with JsHelper in CakePHP 2.0, I got a Request method to update the value of a form field with the results of an AJAX call - so far, so good. However, on inspecting the form field it says: <input type="hidden" id="JobsPartUnitcost" na

  • VBA to Retain the content of some Word Form fields while clearing the content of all remaining Form fields 2013-07-13

    Hello, I have a protected Word Form that contains a variety of form fields. The document is storing project information. At the one year anniversary, most of the entries in the fields need to be reevaluated and thus blanked out, but some of the field

  • django templates: How to know form field type and add any buttons based on field type 2013-12-06

    I am displaying my django forms dynamically with below code. {% for field in form %} {% if field.field.required %} <span class="red">*</span> {% endif %} {{ field.label }}: {{ field }} {% endfor %} Now I want to know the datatype of field

  • function to disable/enable dynamically generated form fields 2011-05-09

    I'm no javascript expert and i'm currently trying to create a function for a form that has the same fields repeated depending on a number selected on a previous page. There could be between 1 and 10 rows of the form fields with each having a radio bu

  • Creating C# form fields with events based off of a text file 2010-06-07

    I need to generate windows form fields based off of a text file that I am parsing. I also want to automatically generate validation event handlers which all will be basically the same only the form field and the regular expression which is used to va

  • Generating Form Fields from XML Tags 2011-02-24

    I'm trying to figure out a way to utilize a PHP script that will: Open an XML document when a link to that document is clicked (from an HTML page). Scan the XML document for tags. Create an HTML form with input fields based on the tags. Post the inpu

  • JQuery/Javascript set value of form field with generated ID 2011-06-09

    On my page I have many forms, in witch field ids are generated based on DB id. like this: <input type="hidden" name="id" value="<?php echo (isset($content))?$content->getId():''?>" /> <input type="hidden" id="x<?php echo $content

  • How to grab a jquery-generated form field value where the id includes a dynamically generated int --using PHP 2011-07-08

    So I'm working on an object-oriented PHP website that uses the MVC model. In my view, I have the following code that is generating a form: //Handle add custom module button click $('.custom-btn').click(function() { var id = $('.custom-fieldset').leng

  • Dynamically fill a form field based on iframe ID 2011-07-25

    I've seen lots of articles about how to pass values from a form field to an iframe - but none the other way round - So can this be done? I'm calling an external form from another domain (another site we own and have access to) in an iframe like this:

Copyright (C), All Rights Reserved.

processed in 0.180 (s). 11 q(s)