0

This is a simple problem and I've look at all answer(There is a quite a few) that are related and compared my code to it and I can't see where I'm going wrong.

Controller

        [HttpGet]
        public ActionResult PatInformation(long id)
        {
            if (ModelState.IsValid)
            {
                var patient = db.tblPatients.Find(id);

                PatientViewModels patientVM = _mapper.Map<PatientViewModels>(patient);

                return View(patientVM);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PatientInformation(PatientViewModels model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            tblPatient tblPatient = db.tblPatients.Find(model.PatientID);

            tblPatient.Forename1 = model.Forename1;
            tblPatient.Surname = model.Surname;

            db.Entry(tblPatient).State = EntityState.Modified;
            db.SaveChanges();

            PatientViewModels patientsVM = _mapper.Map<PatientViewModels>(tblPatient);

            return View("PatientInformation", patientsVM);

ViewModel

    public class PatientViewModels
    {
        public PatientViewModels()
        {
            tblTumours = new List<TumoursViewModel>();
            tblGP = new tblGP();
            tblAddress = new tblAddress();
        }

        public long PatientID { get; set; }

        public long? HSCNO { get; set; }

        public string Surname { get; set; }
        public string Forename1 { get; set; }
        public string Forename2 { get; set; }
        public string PrevName { get; set; }
        public long? PatAddress { get; set; }
        public long? PatGP { get; set; }
        public string BirthGender { get; set; }
        public DateTime? DOB { get; set; }
        public double? Ethnic { get; set; }
        public string VitalStatus { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateAmended { get; set; }
        public double? OldID { get; set; }
        public long? NICRNumber { get; set; }
        public virtual tblAddress tblAddress { get; set; }
        public virtual tblGP tblGP { get; set; }
        public virtual List<TumoursViewModel> tblTumours { get; set; } 
        public string FullAddress { get; set; }
        public string Age { get; set; }
    }

    public class TumoursViewModel
    {
        public long TumourID { get; set; }
        public Nullable<double> TumourNo { get; set; }
        public Nullable<long> PatientID { get; set; }
        public string ICD03 { get; set; }
        public string ICD10 { get; set; }
        public virtual tblMorphology tblMorphology { get; set; }
        public virtual tblBehaviour tblBehaviour { get; set; }
        public virtual tblAddress tblAddress { get; set; }
        public Nullable<System.DateTime> DateOfDiag { get; set; }
        public string Metastases { get; set; }
        public string TumourStatus { get; set; }
        public Nullable<double> StageGrade { get; set; }
        public PatientViewModels Patients { get; set; }
    }
<table id="tblTumours" class="table">
    <thead>
        <tr>
            <th>Tumour Id</th>
            <th>ICD03 Site</th>
            <th>ICD10 Site</th>
            <th>Morphology</th>
            <th>Behaviour</th>
            <th>Diagnosis</th>
            <th>Method</th>
            <th>Status</th>
            <th>Final Stage / Complate</th>
        </tr>
    </thead>
    <tbody>

        @for (int i = 0; i < Model.tblTumours.Count; i++)
        {
        <tr class="table-row" data-href="@Url.Action(" TumourInformation", "Tumours" , new { tumourId=Model.tblTumours[i].TumourID, patientId=Model.PatientID })">
            <td class="pt-3-half" hidden="true">@Html.TextBoxFor(m => Model.tblTumours[i].TumourID, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].TumourNo, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.HiddenFor(m => Model.tblTumours[i].ICD03, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].ICD03, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].ICD10, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].tblMorphology.morphology_code, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].tblBehaviour.BehaviourCode, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].DateOfDiag, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].Metastases, new { Class = "form-control" })</td>
            <td class="pt-3-half"><span class="badge badge-warning">@Html.TextBoxFor(m => Model.tblTumours[i].TumourStatus, new { Class = "form-control" })</span></td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].StageGrade, new { Class = "form-control" })</td>
        </tr>
        }


    </tbody>
</table>

When I post the controller with tblTumours List always returns the as count=0. I'm sure this is a simple fix but I just can't see it.

Hopefully someone can help and please bare with as I just started as a developer and inherited this. Thanks!

3
  • tblTumours = new List<TumoursViewModel>(); ? you probably don't need to do this Commented Jun 24, 2020 at 21:19
  • You probably need an Include() somewhere. Pls add the controller action that returns the view Commented Jun 24, 2020 at 21:28
  • because you are creating new instance List in you constructor get rid of it. when you are posting some data in object, it creates new instance. in case of you PatientViewModels will be created and alse include <table id="tblTumours" class="table"></table> in form tag Commented Jun 24, 2020 at 21:29

1 Answer 1

0

Try this

@using (Html.BeginForm("PatientInformation", "YourController", FormMethod.Post))
{
<table id="tblTumours" class="table">
    <thead>
        <tr>
            <th>Tumour Id</th>
            <th>ICD03 Site</th>
            <th>ICD10 Site</th>
            <th>Morphology</th>
            <th>Behaviour</th>
            <th>Diagnosis</th>
            <th>Method</th>
            <th>Status</th>
            <th>Final Stage / Complate</th>
        </tr>
    </thead>
    <tbody>

        @for (int i = 0; i < Model.tblTumours.Count; i++)
        {
        <tr class="table-row" data-href="@Url.Action(" TumourInformation", "Tumours" , new { tumourId=Model.tblTumours[i].TumourID, patientId=Model.PatientID })">
            <td class="pt-3-half" hidden="true">@Html.TextBoxFor(m => Model.tblTumours[i].TumourID, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].TumourNo, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.HiddenFor(m => Model.tblTumours[i].ICD03, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].ICD03, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].ICD10, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].tblMorphology.morphology_code, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].tblBehaviour.BehaviourCode, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].DateOfDiag, new { Class = "form-control" })</td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].Metastases, new { Class = "form-control" })</td>
            <td class="pt-3-half"><span class="badge badge-warning">@Html.TextBoxFor(m => Model.tblTumours[i].TumourStatus, new { Class = "form-control" })</span></td>
            <td class="pt-3-half">@Html.TextBoxFor(m => Model.tblTumours[i].StageGrade, new { Class = "form-control" })</td>
        </tr>
        }


    </tbody>
</table>
}

public class PatientViewModels
{
    public PatientViewModels()
    {           
       tblGP = new tblGP();
       tblAddress = new tblAddress();
    }

 // Your code
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mentioning the constructor!. You both were right(@jeff and @west). I already had the BeginForm but just never posted the whole view(probably should have) and I skimmed down and noticed an additional <form></form> before the table which also caused the issue.. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.