Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:46:06+00:00 2026-06-11T18:46:06+00:00

I have an example project, a dynamic questionnaire system where any administrator can create

  • 0

I have an example project, a dynamic questionnaire system where any administrator can create a questionnaire, then add groups of questions to it and in-turn add questions to each question group.

Take the following group of POCOs that make up the entities for my EF data context:

public class Questionnaire
{
    public virtual int Id { get; set; }
    public virtual string QuestionnaireName { get; set; }
    public virtual IList<QuestionGroup> QuestionGroups { get; set; }
}

public class QuestionGroup
{
    public virtual int Id { get; set; }
    public virtual string GroupName { get; set; }
    public virtual int QuestionnaireId { get; set; }
    public virtual IList<Question> Questions { get; set; }
}

public class Question
{
    public virtual int Id { get; set; }
    public virtual string QuestionText { get; set; }
    public virtual int QuestionGroupId { get; set; }
    public virtual QuestionGroup QuestionGroup { get; set; }
}

I am accessing these entities in my Web UI via WCF Data Services and am wondering what a best practice (or at least a cleaner way) of handling input in my view for these entities. The following are some of the ideas I have of overcoming this, but I’m having a hard time liking any of them because they just feel a but convoluted.

Solution 1

Add a property to my Question entity called SubmittedValue and have my EF data context Ignore(m => m.SubmittedValue) this. This property is what I will use to persist the input value for the Question at the view level.

What I don’t like about this, is bloating my POCO entity with pretty much irrelevant properties – I’m only going to use SubmittedValue in one case at the Web UI whereas my POCO entities will be used elsewhere many times.

Solution 2

Create view model objects that have the same structure as my POCOs, let’s call them QuestionnaireModel, QuestionGroupModel and QuestionModel – these are initialised in my controller and properties are copied from the POCO to the view model. On QuestionModel I add my SubmittedValue property and persist this value using a custom model binder which looks at the binding context and gets my values from the view – where the name looks something like [group.question.1] (where 1 is the Id of the question). This is presented in the view using Editor Templates for each question group and for each question.

What I don’t like about this, is bloating my web UI with these extra view model objects and having to manually copy property values from my POCO to the view model. I’m aware I could use something like AutoMapper to do this for me, but this is just automating that work, where I’d ideally like to not do it at all.

Solution 3

Change solution 2 slighly, to instead extend my POCOs and override the virtual collection properties with the other view model objects. So, my view model would look like this:

public class QuestionnaireModel : Questionnaire
{
    public new IList<QuestionGroupModel> QuestionGroups { get; set; }
}

public class QuestionGroupModel : QuestionGroup
{
    public new IList<Question> Questions { get; set; }
}

public class QuestionModel : Question
{
    public string SubmittedValue { get; set; }
}

I do like this idea the best, but I haven’t actually tried this yet. I get the best of both worlds here as 1. I can keep my POCOs out of my views and 2. I keep that one-time use property SubmittedValue out of my business layer.

Do any of you have a better way of handling this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:46:07+00:00Added an answer on June 11, 2026 at 6:46 pm

    Having played around with solution 3 (which was my preferred solution) I’ve managed to finally get it. Here’s what I’m doing, for anyone who stumbles on this question. First, I create my view models that extend my POCO entities. I override the collection properties with a new implementation to make my collections my view model types. I then add my form persistence property to my Question view model (so I can keep it out of my business layer).

    public class QuestionnaireModel : Questionnaire
    {
        public new IList<QuestionGroupModel> QuestionGroups { get; set; }
    }
    
    public class QuestionGroupModel : QuestionGroup
    {
        public new IList<QuestionModel> Questions { get; set; }
    }
    
    public class QuestionModel : Question
    {
        public string SubmittedValue { get; set; }
    }
    

    Using AutoMapper I create the mappings between my POCOs and view models like so (using .AfterMap() to make sure my persistence property isn’t a null but an empty string instead):

    Mapper.CreateMap<Questionnaire, QuestionnaireModel>();
    Mapper.CreateMap<QuestionGroup, QuestionGroupModel>();
    Mapper.CreateMap<Question, QuestionModel>().AfterMap((s, d) => d.SubmittedValue = "");
    

    Next, each Question has an editor template that has a single input element:

    @Html.Raw(string.Format("<input type=\"text\" name=\"group.question.{0}\" value=\"{1}\" />", Model.Id.ToString(), Model.SubmittedValue)
    

    Finally, I pick up (and persist) these values using a custom model binder, like so:

    int id = Int32.Parse(controllerContext.RouteData.Values["id"].ToString());
    
    var questionnaire = _proxy.Questionnaires
        .Expand("QuestionGroups")
        .Expand("QuestionGroups/Questions")
        .Where(q => q.Id == id)
        .FirstOrDefault();
    
    var model = Mapper.Map<Questionnaire, QuestionnaireModel>(questionnaire);
    
    foreach (var group in model.QuestionGroups)
    {
        foreach (var question in group.Questions)
        {
            string inputValueId = "group.question." + question.Id.ToString();
            string value = bindingContext.ValueProvider.GetValue(inputValueId).AttemptedValue;
    
            question.SubmittedValue = value;
        }
    }
    

    Although I’m not too happy about the custom model binder (I don’t think I’m setting my editor templates up correctly so resorting to a custom binder) for me this is the preferred solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1.I have successfully run the ScanTest example project in my xcode4.2.1 2.And then I
I'd like to have a dynamic messaging system in my C++ project, one where
i'm download DrillDownApp example project from iPhoneSDKArticles i have a problem when i'm try
I need to have : http://www.example.com/v1/my-project/ redirected to http://example.com/my-project/ so : (1) remove the
I wants to access a method in different project. as example i have called
I have a simple java project (adapted from the example here ), which is
I have been looking at the example posted on OpenNTF - http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=Threads%20and%20Jobs my issue
I have an ant script with the following header: <project name=Simple ActiveJDBC Example default=clean
I have a project ongoing at the moment that uses dynamic text blocks to
I have an dynamic array, containing for example (int) {1, 2, 3} I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.