1

Not sure how to word this...

I have a Model, here is a section of it:

public class AnswerSheet
    {

public string Q1 { get; set; }
public string Q2 { get; set; }
public string Q3 { get; set; }
public string Q4 { get; set; }

I am using a Viewmodel to reuse the same view to answer each question separately. It is almost working. Is there any way I can use my controller as follows to dynamically assign the model.q#, ex:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateNextQ([Bind(Include = "ID, qCounter, Question,Comment")] AnswerSheetCreateVM answerVM)
        {
            if (ModelState.IsValid)
            {
                string questionAns = answerVM.Question + answerVM.Comment;
                AnswerSheet answer= db.AnswerSheets.Find(answerVM.ID);


//THIS PART HERE IS WHERE I HAVE A PROBLEM
                answer.Q(answerVM.qCounter) = questionAns;
//That one line above
                db.AnswerSheets.Add(answer);
                db.SaveChanges();

So basically can I get data from my controller variable (qCounter in this case) and assign it to my model like Model.Q(qcounter)

As a side note I am open to suggestion on how to word this question or what tags to assign to it.

dave317
  • 754
  • 2
  • 12
  • 30
  • 3
    It sounds like your model for `AnswerSheet` should really have a `List Questions { get; set; }` – Jon Skeet Jul 25 '16 at 20:30
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. –  Jul 25 '16 at 23:10
  • Jon Skeet, the thing is, I want my data going back into a database of AnswerSheets where each record has colums for each question (Q1, Q2, Q3, etc). Is there still a way I could bind my model to a list but have the data save this way in the database? I'm using EF6 code first. – dave317 Jul 26 '16 at 00:49
  • I will use a list and see how it goes into the database, apologies I should have done that before I asked the above comment. – dave317 Jul 26 '16 at 00:50
  • I'm stuck on how to do this with EF6 Code first....will post if I find a way that dosn involve writing a new controller action for each question (Q1, Q2, Q3) where the integer is the only thing that changes.... – dave317 Jul 26 '16 at 03:08
  • It looks like I could do this with Reflection from what I have been reading but I think this will degrade performance too much...just going with the classic method – dave317 Jul 26 '16 at 03:19

1 Answers1

0

I found this post useful:

Set object property using reflection

This is what I ended up doing, still have to test performance:

string test = string.Format("Q" + answerVM.qCounter);                    
                string questionAns = (answerVM.Question + " - " + answerVM.NoComment);

                AnswerSheet answer= db.AnswerSheets.Find(answerVM.ID);
                if (answer== null)
                {
                    return HttpNotFound();
                }                
                answer.GetType().GetProperty(test).SetValue(answer, questionAns, null);
db.Entry(answer).State = EntityState.Modified;
                db.SaveChanges();

Maybe that will help someone down the line....NOt sure if this counts as using reflection....

Community
  • 1
  • 1
dave317
  • 754
  • 2
  • 12
  • 30