-2

I encountered a null exeception when i tried using this method and i dont know what is wrong.

Your help will be of great assistance thanks.

HttpPost

     [HttpPost]
     public ActionResult AddConsultation(Consultation _conmodel, int PatientId, int[] courses)
        {
            DataContext da = new DataContext();
            if (courses != null)
            {
                foreach (var id in courses)
                {
                    Symptoms course = da.Symptoms.Find(id);

                    _conmodel.Symptomses.Add(course);

                }
            }
            da.Consultations.Add(_conmodel);
            da.SaveChanges();
            return View("Index");
        }

I'm getting null exception on _conmodel.Symptomses.Add(course);

This is my view

  <div class="form-group">

                @Html.LabelFor(model => model.Symptomses, htmlAttributes: new {@class = "control-label col-md-2"})
                <div class="col-md-10">
                    @Html.ListBox("courses")
                    @Html.ValidationMessageFor(model => model.Symptomses, "", new {@class = "text-danger"})
                </div>
                <br/>
                <br/>
                <hr/>
            </div>
            <div class="form-group">

                <br/>
                <hr/>
                @Html.LabelFor(m => m.illness, new {@class = "col-md-2 control-label"})
                <div class="col-md-10">
                    @Html.TextAreaFor(m => m.illness, new {rows = "4", cols = "50", htmlAttributes = new {@class = "form-control"}})
                </div>

            </div>
        </div>

        <div class="col-md-6">
            <h4 class="x"></h4>
            <hr/>
            <div class="form-group">
                @Html.LabelFor(m => m.PresribedMed, new {@class = "col-md-2 control-label"})
                <div class="col-md-10">
                    @Html.TextAreaFor(m => m.PresribedMed, new {rows = "4", cols = "50", htmlAttributes = new {@class = "form-control"}})
                </div>
                <br/>
                <br/>
                <hr/>
            </div>
            <br/>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" class="btn btn-primary" value="Add Consultation"/>

                    @*<button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Next", new { id = Model.UserName })';return false;"> Next</button>*@
                </div>
            </div>
        </div>
    }
</div>

<script type="text/javascript">
    $("#courses").select2({
        placeholder: "Please select symptoms",
        maximumSelectionSize: 10,
        width:300
    });
</script>

1 Answers1

1

_conmodel.Symptomses is probably null. Assuming Symptomses is a list, try this instead:

if (courses != null)
{
    _conmodel.Symptomses = _conmodel.Symptomses ?? new List<Symptomses>();
    foreach (var id in courses)
    {
        Symptoms course = da.Symptoms.Find(id);
        _conmodel.Symptomses.Add(course);

    }
}

This way, we can check if _conmodel.Symptomses is null, and if it is, instantiate it.

ediblecode
  • 10,724
  • 16
  • 62
  • 111