0

I have an app for creating Exams. After creating a new question I would like to pass the question_id parameter to the questions_choices create action. I have created a list of questions choices to do 4 at a time (A, B, C, & D) in the action. I am having difficultly in passing the question_id into the list.

Controller:

    public ActionResult CreateNewChoice(int? id)
    {
        var questions_choices = new List<questions_choices>();

        for (int i = 0; i < 4; i++)
        {
            questions_choices.Add(new questions_choices());               
        }                 
        return View(questions_choices);
    }

I would like to implement something like this within the list but a little confused how.

questions_choices questions_choices = new questions_choices();
        questions_choices.questions_id = id;

Model

 public partial class questions_choices
{
    public long questions_choices_id { get; set; }
    public Nullable<int> questions_id { get; set; }
    public string questions_choices_value { get; set; }
    public string questions_choices_string { get; set; }  
}

If a put the two together I have issues with the variable being declared twice.

5
  • What part are you actually having difficulty with? Commented Mar 6, 2017 at 16:42
  • can you provide questions_choices model? Commented Mar 6, 2017 at 16:50
  • Added Model to description Commented Mar 6, 2017 at 16:53
  • you mean you want to assign 1,2,3,4 to questions_id seperately? Commented Mar 6, 2017 at 16:58
  • same questions_id for all 4 Commented Mar 6, 2017 at 16:59

2 Answers 2

2

you can try something like this

 public ActionResult CreateNewChoice(int? id)
    {
        var questions_choices = new List<questions_choices>();

        for (int i = 0; i < 4; i++)
        {
            questions_choices.Add(new questions_choices(){questions_id = id});               
        }                 
        return View(questions_choices);
    }

it will assign id of the parameter to the questions_id to all 4 questions

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

1 Comment

Thanks for your help, I didn't know you could pass parameters that way
0
    public ActionResult CreateNewChoice(int? id)
    {
    var questions_choices = new List<questions_choices>();
    questions_choices Objquestion;
    for (int i = 0; i < 4; i++)
    {
        Objquestion = new questions_choices();
        Objquestion.questions_id = id;
        questions_choices.Add(Objquestion);               
    }                 
    return View(questions_choices);
    }

Comments

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.