3

I have the need to display a group of questions on a form, there could be one question or many questions, and the answers to the questions could be of different types (for instance, Age, Name, date of birth etc).

So far what I've managed to come up with is a View Model:

public class QuestionViewModel
{
   public List<QuestionType> Questions { get; set; }
}

It displays a List of type QuestionType:

public class QuestionType
{
    public int QuestionID { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

What I need to know is, is it possible to specify something on the property that would allow me to change the type? I have the feeling it isn't possible, so failing that, are there any suggestions as to how I can deal with this, keeping it as inline with MVC as possible?

The reason I want to do this is so that it hooks up into the default MVC framework validation and will validate it to the correct type, as an example writing "Hello" into a question that is asking for "Age".

I have an idea for a workaround if it isn't possible where I store the type information in the model as such:

public class QuestionType
{
    public int QuestionID { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
    public string TypeInfo { get; set; }
}

and using the information stored there to write custom validation logic.

11
  • 1
    When you say change type do you mean a string to int or something like a category switch in which case you can look at '"Enums"'.
    – Dr Schizo
    Oct 29, 2014 at 15:38
  • What I mean is, say for instance the question comes in and it is "What is your name?" I'd like the type of my Answer property to be a string. If the question comes in and it is "What is your age?" I want my Answer property to be an integer. I was basically hoping that there was a way in mvc to do this without having to do a workaround, which I'm fully prepared to do if needs be.
    – Enbern
    Oct 29, 2014 at 15:39
  • Have you tried using DataAnnotation regular expressions? This won't change the type per say, but it will allow you to validate the input without directly making changes to your model. IE for age you set the RegEx to look for only numbers, anything else throws an error Oct 29, 2014 at 15:41
  • 1
    Writing a custom DataAnnotation is most definitely an option, the problem is, it would require me to write a lot of logic in order to verify what the Answer type should be and thus how it should validate. I am prepared to do this I was just hoping there is an easier, quicker or more native route to doing this.
    – Enbern
    Oct 29, 2014 at 15:43
  • 1
    This might help stackoverflow.com/questions/11486286/…
    – CSharper
    Oct 29, 2014 at 17:50

1 Answer 1

2

Change your Answer property to an object:

public class QuestionType
{
    public int QuestionID { get; set; }
    public string Question { get; set; }
    public object Answer { get; set; }
}

Use the object:

public void HandleAnswer(QuestionType qt)
{
    if (qt.Answer is Boolean)
    {
        //do boolean stuff
    }
    else if (qt.Answer is String)
    {
        //do string stuff
    }
    else if (qt.Answer is Int32)
    {
        //do int stuff
    }

    //do unknown object stuff

}
1
  • I'm going to mark this as the answer, and provide this link: stackoverflow.com/questions/11486286/… that was given to me by the user CSharper. Using a combination of these two, it allows me to cast the type so I receive default client-side validation, and using something similar to your method to do my server side checks. Thanks a lot.
    – Enbern
    Oct 31, 2014 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.