-2

このメソッドを使用しようとしたときに null 例外が発生しましたが、何が問題なのかわかりません。

あなたの助けは大きな助けになります。

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");
        }

_conmodel.Symptomses.Add(course);で null 例外が発生します。

これが私の見解です

  <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>
4

1 に答える 1

1

_conmodel.Symptomsesおそらくnullです。がリストであると仮定Symptomsesして、代わりにこれを試してください:

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);

    }
}

このようにして、 null かどうかを確認し、 _conmodel.Symptomsesnull の場合はインスタンス化できます。

于 2015-06-04T14:40:36.993 に答える