0

Well! I'm using ASP.MVC5. What I'm trying to do is to loop an array that contains objects and each object contains array properties:

I have this class:

public class MylistModels  
{
  public string Subtitle { get; set; }
  public string[] Question { get; set; }
}

The logic is:
I have a POST form in HTML (I can add more fields if I want):

<input type="text" placeholder="Subtitle here" name="lists[0].Subtitle" />
<input type="text" placeholder="Subtitle here" name="lists[0].Question" />
<input type="text" placeholder="Subtitle here" name="lists[0].Question" />
<input type="text" placeholder="Subtitle here" name="lists[1].Subtitle" />
<input type="text" placeholder="Subtitle here" name="lists[1].Question" />
<input type="text" placeholder="Subtitle here" name="lists[1].Question" />
<input type="text" placeholder="Subtitle here" name="lists[2].Subtitle" />
<input type="text" placeholder="Subtitle here" name="lists[2].Question" />
<input type="text" placeholder="Subtitle here" name="lists[2].Question" />

When I click in save I send it to an action:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(List<MylistModels> lists)
{
    if (ModelState.IsValid)
    {
        //how to loop the arrays that contains a subtitle with questions array?

        return RedirectToAction("Index");
    }                        
    return View();
}

When I receive it in the action, it looks like this:
I receive an array of objects and each one has your subtitle related to your questions
Obs.: the "subtitle" is always one in each object and "questions" I can have one or more.

PROBLEM:
How can I loop inside each object array and loop again in each question of this object?

I'm trying to do like this but it's not working:

SubtitleChecklist subtitleCheckList = new SubtitleChecklist();            
QuestionChecklist questionChecklist = new QuestionChecklist(); 

foreach (var list in lists)
{
    subtitleCheckList.IdChecklist = idChecklist;
    subtitleCheckList.Subtitle = list.Subtitle;

    db.subtitleCheckList.Add(subtitleCheckList);
    db.SaveChanges();

    int idSubtitleChecklist = subtitleCheckList.Id;

    for (int i = 0; i < list.Question.Length; i++)
    {
        questionChecklist.Question = list.Question[i];
        questionChecklist.IdSubtitle = idSubtitleChecklist;

        db.QuestionChecklist.Add(questionChecklist);
        db.SaveChanges();
    }
}
3
  • 1
    Can you explain why it's not working? Did you debug it? What is the result? Commented Nov 4, 2015 at 18:47
  • Where are you setting idChecklist? Commented Nov 4, 2015 at 19:01
  • This is from another part of the code, it's ok in my code. Commented Nov 4, 2015 at 19:05

2 Answers 2

1

Where are you instantiating questionChecklist and subtitleCheckList?

It looks like you are modifying the same objects over and over in your loops.

UPDATE 1:

Move: SubtitleChecklist subtitleCheckList = new SubtitleChecklist(); inside the outer loop and the QuestionChecklist questionChecklist = new QuestionChecklist(); into the inner loop.

Also, what result are you getting now?

Sign up to request clarification or add additional context in comments.

1 Comment

I did it. I forgot to put this code. My problem is the looping. Do you have any idea?
0

You loop through lists object but you assign values and do all other stuff to one and the some object that you defined outside the loop and has nothing to do with your loop.

1 Comment

Each subtitle has many questions. So when I insert a subtitle, I have to keep the ID to use for the question related to the subtitle.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.