POST 控制器操作方法有一个 LIST return,count=0
POST controller action method is having a LIST return with count=0
这是一个简单的问题,我查看了所有相关的答案(有很多)并将我的代码与它进行了比较,但我看不出我哪里出错了。
控制器
[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>
当我 post 带有 tblTumours 列表的控制器总是 returns 计数=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
}
这是一个简单的问题,我查看了所有相关的答案(有很多)并将我的代码与它进行了比较,但我看不出我哪里出错了。
控制器
[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>
当我 post 带有 tblTumours 列表的控制器总是 returns 计数=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
}