【问题标题】:ASP.NET Core MVC Model Binding Problem Returning Null ValueASP.NET Core MVC 模型绑定问题返回 Null 值
【发布时间】:2021-08-23 06:55:05
【问题描述】:

我正在开发一个使用如下视图模型的 ASP.NET Core 5 MVC Web 应用程序:

    public class Form3040VM
    {
        public tblPatientVisits patientVisit = new tblPatientVisits();
        public tblPtmstr1 patient = new tblPtmstr1();
        public tbl_Log_Vitals patientVitals = new tbl_Log_Vitals();
        public FHSAAFindings findings = new FHSAAFindings();
    }

患者类别是:

    public class tblPtmstr1
    {
        [Key]
        public int PatientID { get; set; }    

        [Display(Name = "Patient Last Name")]
        public string PatientLastName { get; set; }

        [Display(Name = "Patient First Name")]
        public string PatientFirstName { get; set; }
}

表单呈现:

<input  id="patient_PatientFirstName" name="patient.PatientFirstName">

正如预期的那样。表单将patient.PatientFirstName 的值发布到控制器

public IActionResult FHSAAPreviewSubmit(Form3040VM model)
{
    if (ModelState.IsValid)
    {
        try
        {
            return Content(model.patient.PatientFirstName ?? "Null Value");
        }
        catch (Exception ex)
        {
            return Content(ex.ToString());
        }    
    }
    else
    {
        return Content("Invalid Model");
    }             
}

控制器将patient.PatientFirstName 返回为空,即使控制台清楚地显示了正在发布的值。但是,当我使用 IFormCollectionRequest.Form["patient.PatientFirstName"] 时,值会正确返回。

我不确定是什么问题。任何帮助将不胜感激。

【问题讨论】:

    标签: .net asp.net-core-mvc


    【解决方案1】:

    控制器将 patient.PatientFirstName 返回为 null,即使控制台清楚地显示了正在发布的值。但是,当我使用 IFormCollection 和 Request.Form["patient.PatientFirstName"] 时,值会正确返回。我不确定是什么问题。

    请尝试修改您的视图模型类Form3040VM 以使用getset 访问器将patient 等定义为属性,如下所示。

    public class Form3040VM
    {
        //...
        public tblPtmstr1 patient { get; set; }
        //...
    

    然后模型绑定系统将帮助从发布的数据中查找值并绑定到这些公共属性。有关什么是模型绑定及其工作原理的更多信息,您可以查看此文档:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0

    【讨论】:

      【解决方案2】:

      所有这些新的目的是什么?我以前从未见过这样的事情

      public class Form3040VM
          {
              public tblPatientVisits patientVisit = new tblPatientVisits();
              public tblPtmstr1 patient = new tblPtmstr1();
              public tbl_Log_Vitals patientVitals = new tbl_Log_Vitals();
              public FHSAAFindings findings = new FHSAAFindings();
          }
      

      您刚刚创建了空对象并且没有提供任何设置器来从提交中获取任何数据

      尝试改用这个

      public class Form3040VM
          {
              public tblPatientVisits patientVisit {get; set;}
              public tblPtmstr1 patient {get; set;}
              public tbl_Log_Vitals patientVitals {get; set;}
              public FHSAAFindings findings {get; set;}
          }
      

      并修复视图控件绑定

      
      @model Form3040VM
      
      <form method="post" action="...">
      
      ....
      <input asp-for="@Model.tblPtmstr1.PatientFirstName"  class="form-control"  />
      .... and so on for all controls
      
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多