c# - How to fill a list of objects that contain a list of string from view to controller in asp.net viewModels -


i using asp.net mvc5 store list of questions contain:

  1. the questions itself
  2. the correct answer
  3. list of wrong answers.

i not sure codes needed accommodate needs in view pass required data form list<question>jobquestions in jobpostviewmodel.

these codes have done :

public class jobpostviewmodel {     public class question     {         public string thequestion { get; set; }         public string rightanswer { get; set; }         public list<string> wronganswers { get; set; }     }      public list<question>jobquestions { get; set; } } 

i have tried many ways such as:

<div class="form-group">      <div class="col-md-10">           @html.textboxfor(model => model.jobquestions.firstordefault().thequestion, new { @class = "form-control", })           @html.textboxfor(model => model.jobquestions.firstordefault().rightanswer, new { @class = "form-control", })           @html.textboxfor(model => model.jobquestions.firstordefault().wronganswers, new { @class = "form-control", })           @html.textboxfor(model => model.jobquestions.firstordefault().wronganswers, new { @class = "form-control", })           @html.textboxfor(model => model.jobquestions.firstordefault().wronganswers, new { @class = "form-control", }) </div> 

but not work expected.

i know :

public class jobpostviewmodel {     public string question1 { get; set;}     public string correctanswer1 { get; set;}     public list<string>incorrectanswers1 { get; set;}     public string question2 { get; set;}     public string correctanswer2 { get; set;}     public list<string>incorrectanswers2 { get; set;} } 

and view need do:

@using (html.beginform())  {     @html.textboxfor(model => model.question1, new { @class = "form-control", })     @html.textboxfor(model => model.correctanswer1 , new { @class = "form-control", })     @html.textboxfor(model => model.incorrectanswers1 , new { @class = "form-control", })     @html.textboxfor(model => model.incorrectanswers1 , new { @class = "form-control", })     @html.textboxfor(model => model.incorrectanswers1 , new { @class = "form-control", })      @html.textboxfor(model => model.question2, new { @class = "form-control", })     @html.textboxfor(model => model.correctanswer2 , new { @class = "form-control", })     @html.textboxfor(model => model.incorrectanswers2 , new { @class = "form-control", })     @html.textboxfor(model => model.incorrectanswers2 , new { @class = "form-control", })     @html.textboxfor(model => model.incorrectanswers2 , new { @class = "form-control", })       <p>        <input type="submit" value="save" />    </p> } 

but not idea if going collect hundred of questions.

thanks suggestion!

something work, using existing question model.

@using (html.beginform())  {     @html.textboxfor(model => model.question, new { @class = "form-control", })     @html.textboxfor(model => model.correctanswer , new { @class = "form-control", })      @for (int = 0; < model.wronganswers.count; i++)     {         @html.textboxfor(model => model.wronganswers[i], new { @class = "form-control", })     } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -