0

I am trying to send an array from my view to the controller to save it in the database.

The user will select couple check boxes and each one should be saved int its own row.

However,nothing is sending correctly and I get this message.

System.NullReferenceException: Object reference not set to an instance of an object.

I have a very simple table that has the following:

public class Answer
{
    public int Id { get; set; }
    public int OptionID { get; set; }
    public int QuestionID { get; set; } = 1;
}

In the view I have list of check boxes in the form that has values like this:

<input type="checkbox" name="OptionID[0]" value="1" class="single-checkbox"> checkbox 1<br>
<input type="checkbox" name="OptionID[1]" value="2" class="single-checkbox"> checkbox 2<br>
<input type="checkbox" name="OptionID[2]" value="3" class="single-checkbox"> checkbox 3<br>
<input type="checkbox" name="OptionID[3]" value="4" class="single-checkbox"> checkbox 4<br>

In my controller:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(List<Answer> answers)
{
        if (ModelState.IsValid)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                foreach (var i in answers)
                {
                    db.Answers.Add(i);
                }
                await db.SaveChangesAsync();
            }
            return RedirectToAction("Index");
        }
        return View(answers);
}
Fahad
  • 113
  • 2
  • 15
  • Use `name="[0].OptionID"`, `name="[1].OptionID"` etc (but why are you generating the html manually? You should be using a view model generating a strong typed view (refer [Pass List of Checkboxes into View and Pull out IEnumerable](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Aug 16 '18 at 12:16
  • @StephenMuecke, Thank you for your help it worked fine. I was trying to generate it and most of the tutorials weren't actually helpful. but with your comment actually solved two problems for me. Thank you so much. – Fahad Aug 16 '18 at 12:26
  • @StephenMuecke Now it works find if i select the first option alone, first and second option. but if i try anything else it won't work and i get the same error – Fahad Aug 16 '18 at 12:32
  • The code in the link works fine :) I assume you have other code that you have not shown causing the issue, and if what you have shown in the question is the only form controls you have, then it would not work if you selected say 2 and 3 (because collection indexers must be zero based and consecutive) –  Aug 16 '18 at 12:36
  • @StephenMuecke it is the only form controls I have, but I guess it won't work if the collection indexers must be zero – Fahad Aug 16 '18 at 12:39
  • Well there are options is that is the case, such as including hidden inputs for the collection indexer. But since you nnot even binding to a model and generating the html manually, then you may as well just use `name="OptionIDs"` and change the parameter to `IEnumerable OptionIds` (and `OptionIds` will contain a collection of the selected values). But that is an awful way to be generating your view. Study the code in the link so that you are strongly binding to a model –  Aug 16 '18 at 12:47
  • @StephenMuecke I have followed your suggestion and followed the steps in the link. Now I my the model for the answers and two view models one for the options and one for the answers linked to the options view model. The code works fine. now how can I add the option to my options view model .. I tried added it to my DbContext ` public DbSet options { get; set; }` and added the options but they don't show and the loop is empty. – Fahad Aug 16 '18 at 13:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178143/discussion-between-stephen-muecke-and-fahad). –  Aug 16 '18 at 13:41

0 Answers0