0

I had a previous question a few weeks ago that I now understand the answer to, however now I am wanting to take it one step further.

Here are the models I am working with:

public class Test {
    public string author {get;set;}
    public list<TestItem> testItems {get;set;}
}
public clas TestItem{
    public string question {get;set;}
    public string answer {get;set}
}

I have handed my view something like this (A test with 4 items on it) in my previous projects:

Test model = new Test()
{
    Test= new List<TestItem>()
    {
        new TestItem() { },
        new TestItem() { },
        new TestItem() { },
        new TestItem() { }
    }
};
return View(model)

Where I could in my get method in my controller create a test and a SET number of test items to go into that test. HOWEVER, lets say that I want my user to determine how long a test can be, perhaps 3 questions, perhaps 33 questions. HOW can i determine the number of testItems on the client side?

Travis Tubbs
  • 827
  • 1
  • 14
  • 32
  • 1
    Are you wanting to have the use dynamically add new `TestItem` in the view (by clicking an 'Add' button) or are you wanting to have the user enter a number and then create x number of `TestItem` based on that value? –  Dec 17 '16 at 01:33
  • @StephenMuecke I am wanting them to click an add button and it add another question to the form for them to fill in. – Travis Tubbs Dec 19 '16 at 14:03
  • Refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308). I would recommend using the `BeginCollectionItem` option (refer [this answer](http://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for a more complete example) –  Dec 19 '16 at 23:18
  • Super! So in my own attempts I have tried to use the manual way in the past few weeks. So as far as the option 1 is concerned. How is that implemented so that you have a new text box show up when pressing a button and what not? Would you mind possibly doing another fiddle? I dont understand why you would need IEnumerable model type and not just a list like in our previous example with the Test and TestItems. (That is the same scenario that I am doing with this) – Travis Tubbs Dec 20 '16 at 14:25
  • 1
    Its not possible to create a fiddle using `BeginCollectionItem`, but the 2nd link is a full working example. In your case, the main view is `@model Test` and you create a partial `_TestItem.cshtml` with `@model TestItem` with `@using (Html.BeginCollectionItem("testItems"))`. And to generate existing items, use `foreach(var item in Model.testItems) { @Html.Partial("_TestItem", item) }` in the main view –  Dec 20 '16 at 23:12
  • I am trying to work some form validation on it, but when I leave testItems blank and dont press add, it crashes my code on the foreach loop. How do I fix this? it is saying (not a reference of an object) because it is null. I want to have the test items [Required] for at least one, but i dont know how to do that.The error does not occur if I have added any test items. Other than that I have pretty much got it, besides when I want to edit later and remove one.@StephenMuecke – Travis Tubbs Jan 13 '17 at 20:37
  • I kind of just worked around it and supplied the view with 1 test item automatically. – Travis Tubbs Jan 13 '17 at 20:55
  • 1
    See the edits to the [this answer](http://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892?noredirect=1#comment70484424_40541892). If your still having problems, edit this question (or ask a new question) with the relevant code your using. –  Jan 16 '17 at 04:30

0 Answers0