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?

--------------Solutions-------------

You want to use Templated Helpers - Here is a good walkthrough - http://www.hanselman.com/blog/ASPNETMVCDisplayTemplateAndEditorTemplatesForEntityFrameworkDbGeographySpatialTypes.aspx

In the helper itself you can do stuff like:

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

etc

Category:asp.net Time:2018-09-15 Views:1

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

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.512 (s). 13 q(s)