Reputation: 3
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!
Upvotes: 0
Views: 568
Reputation: 384
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
}
Upvotes: 0