[英]POST controller action method is having a LIST return with count=0
這是一個簡單的問題,我查看了所有相關的答案(有很多),並將我的代碼與它進行了比較,但我看不出哪里出錯了。
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);
視圖模型
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>
當我發布帶有 tblTumours 列表的 controller 時,總是返回計數 = 0。 我敢肯定這是一個簡單的修復,但我只是看不到它。
希望有人可以提供幫助,並且在我剛開始作為開發人員並繼承了這一點時,請盡情享受。 謝謝!
嘗試這個
@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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.