0

Initializing the model with multiple arrays.

I have already tried to initialize but its not working.

namespace ArrayTest.Models
{
    public class Question
    {
        public string question { get; set; }
        public string[] option { get; set; }
        public string[] score { get; set; }
    }
}

public class DefaultController : Controller
{

    public ActionResult Index()
    {
        IEnumerable<Question> x = new List<Question>
        {
            new Question(){ question = "Question1", option = { "abc","cde","efg"}, score = { "abc", "cde", "efg" } },
            new Question(){},
            new Question(){},
            new Question(){}
        };
        return View(x);
    }
}

I expect this model to be initialized and sent to the view.

3
  • you can change your code to be something like new Question(){question="Question1", option= new string[3] { "abc","cde","efg"},score=new string[3]{ "abc", "cde", "efg" } as you are trying to initialize an array. So, in C# your have to define the type along with the size. Commented Jun 11, 2019 at 5:59
  • Please try like this option will be better rather then your code public List<Options> options { get;set;} Commented Jun 11, 2019 at 6:28
  • @GurdeepSingh Why? and how come List<T> would be better? You are tight coupling then rather IEnumerable<T> would be better suggestion Commented Jun 11, 2019 at 6:31

2 Answers 2

2

string[] doesn't have an .Add() method, so option = { "abc", "cde", "efg"} won't work. You need to create the array and initialize the array:

var list = new List<Question>
{
    new Question()
    {
        question = "Question1",
        option = new string[] { "abc", "cde", "efg"},
        score = new string[] { "abc", "cde", "efg" }
    },

    new Question(){},
    new Question(){},
    new Question(){}
};
Sign up to request clarification or add additional context in comments.

1 Comment

Or just option = new [] { "abc", "cde", "efg"},
0

Step 1 : Add a constructor in your modal like following,

 public class Question
    {
        public string question { get; set; }
        public string[] option { get; set; }
        public string[] score { get; set; }

        public Question(string question, List<string> option, List<string> score)
        {
            this.question = question;
            this.option = option.ToArray();
            this.score = score.ToArray();

        }
    }

Step 2: Modify your controller method like following,

 IEnumerable<Question> x = new List<Question> {
                new Question("Question1", new List<string>{"cde","efg"}, new List<string> { "abc", "cde", "efg" }),
                new Question("Question2", new List<string>{"cde","efg"}, new List<string> { "abc", "cde", "efg" }) } ;

If you use this List, You can use .Add() method as well.

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.