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.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Enbern
  • 39
  • 7
  • 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 '14 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 '14 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 – Lee Harrison Oct 29 '14 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 '14 at 15:43
  • Essentially I'm trying to find out if there is something built in to the .NET framework/MVC which means I don't have to do this, as my knowledge of the language/framework isn't massive and I don't want to write all of this code to find out there is a one-liner out there that would do it for me, haha. Having done a quite extensive search I think I will end up having to do my custom logic. – Enbern Oct 29 '14 at 15:53
  • The dataannotation regexs are pretty close to one liners if your just validating input to match criteria. http://stackoverflow.com/questions/8244572/dataannotations-validation-regular-expression-in-asp-net-mvc-4-razor-view – Lee Harrison Oct 29 '14 at 16:16
  • The problem I have is that the questions are dynamically added, and I won't know what type I need the question to be before it's loaded which means I can't annotate it on the model directly, it would need to be performed elsewhere, which is what I'm unsure of how to do. – Enbern Oct 29 '14 at 16:19
  • I agree with @DrSchizo .... how about looking @ Enums? You can safely make your TypeInfo property an Enum one. – noobed Oct 29 '14 at 16:40
  • The TypeInfo property isn't the one I'm having an issue with though, my intention for TypeInfo was it to hold the expected result, i.e "int" or "string", and then use the result of that to write custom validation logic. What I really want to do is avoid having to use that altogether, and set the type of my Answer property to be whatever it is that I'm expecting, instead of a hard coded type. – Enbern Oct 29 '14 at 16:46
  • 1
    This might help http://stackoverflow.com/questions/11486286/asp-net-mvc-3-editor-for-dynamic-property – CSharper Oct 29 '14 at 17:50
  • @CSharper That's done near enough what I needed it to do. Thank you very, VERY much. – Enbern Oct 30 '14 at 09:36

1 Answers1

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

}
morganpdx
  • 1,856
  • 2
  • 29
  • 46
  • I'm going to mark this as the answer, and provide this link: http://stackoverflow.com/questions/11486286/asp-net-mvc-3-editor-for-dynamic-property 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 '14 at 09:27