localhost当前无法处理此请求。在ASP.NET控制器中检索来自dbContext的数据。

huangapple go评论69阅读模式
英文:

localhost is currently unable to handle this request. In ASP.NET controller for retrieving data from dbContext

问题

I am using dbContext 实例来检索视图中的所有数据,但出现以下错误

该页面无法正常工作 - 本地主机当前无法处理此请求。HTTP错误500。

我在数据库中有一条记录,但无法检索它。

这是我用于检索所有数据的方法

private readonly DataContext db;

public PatientsData(DataContext _db) {
    db = _db;
}

public IEnumerable<Patients> GetAllPatients() 
{
    var patients = db.Patients.ToList();
    return patients;
}

这是我的 dbContext

public class DataContext : DbContext
{
    //private readonly IConfiguration Configuration;

    public DataContext(DbContextOptions<DataContext> options)
        : base(options)
    {
    }
       
    public DbSet<Patients> Patients { get; set; }
} 

这是我的视图

@model V._3._0.Models.PatientsData
<table>
    @foreach(var patients in Model.GetAllPatients())
    {
        <tr>
            <td>@patients.Id</td>
            <td>@patients.Name</td>
            <td>@patients.Status</td>
            <td>@patients.DateOfAdm</td>
            <td>@patients.DateOfDis</td>
            
        </tr>
    }
</table> 

我无法确定哪里出错了...

英文:

I am using dbContext instance to retrieve all data in the view of asp.net but ending up with this error

> This page isn't working - localhost is currently unable to handle this request. HTTP ERROR 500.

I have a record in the database and unable to retrieve it.

This is my method to retrieve all data

private readonly DataContext db;

public PatientsData(DataContext _db) {
    db = _db;
}

public IEnumerable&lt;Patients&gt; GetAllPatients() 
{
    var patients = db.Patients.ToList();
    return patients;
}

This is my dbContext:

public class DataContext : DbContext
{
    //private readonly IConfiguration Configuration;

    public DataContext(DbContextOptions&lt;DataContext&gt; options)
        : base(options)
    {
    }
       
    public DbSet&lt;Patients&gt; Patients { get; set; }
} 

This is my view

@model V._3._0.Models.PatientsData
&lt;table&gt;
    @foreach(var patients in Model.GetAllPatients())
    {
        &lt;tr&gt;
            &lt;td&gt;@patients.Id.&lt;/td&gt;
            &lt;td&gt;@patients.Name&lt;/td&gt;
            &lt;td&gt;@patients.Status&lt;/td&gt;
            &lt;td&gt;@patients.DateOfAdm&lt;/td&gt;
            &lt;td&gt;@patients.DateOfDis&lt;/td&gt;
            
        &lt;/tr&gt;
    }
&lt;/table&gt; 

I am unable to spot where I am going wrong ...

答案1

得分: 1

以下是翻译好的部分:

  1. 此页面无法正常运行 - 本地主机当前无法处理此请求。HTTP错误500。

您分享的信息不足以解释您遇到的错误。甚至不确定您是否对IIS或浏览器链接进行了任何其他配置。

但是,我发现您分享的代码中存在一些不一致之处,可能无法按预期工作。

让我们来看看如何获取患者列表,以此来改进您的代码的哪个部分。

假设您有以下模型:

public class Patients
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Status { get; set; }
    public DateTime DateOfAdm { get; set; }
    public DateTime DateOfDis { get; set; }
}
  1. 控制器中的不正确操作:

您试图在视图中显示患者列表,但您使用的是IEnumerable&lt;Patients&gt;,而没有返回视图,而只是使用return patients;,这肯定不会生成视图HTML。

您应该将其修复如下:

public ActionResult GetAllPatients()
{
    var patients = db.Patients.ToList();
    return View(patients);
}
  1. 视图中的错误模型引用:

您在视图中使用了@model V._3._0.Models.PatientsData的引用,但您返回的是Patients.ToList(),应该是Patients类的类型。因此,您的引用应该如下:

@model IEnumerable&lt;YourProjectName.Models.Patients&gt;
  1. 错误的foreach循环调用:

由于您的控制器发送IEnumerable&lt;Patients&gt;,而@model引用将包含它,所以您不需要调用Model.GetAllPatients(),也不能这样做,这是不正确的语法。

相反,您应该按照以下方式编写:

@model IEnumerable&lt;DotNet6MVCWebApp.Models.Patients&gt;
<table>
    @foreach (var patients in Model)
    {
        <tr>
            <td>@patients.Id.</td>
            <td>@patients.Name</td>
            <td>@patients.Status</td>
            <td>@patients.DateOfAdm</td>
            <td>@patients.DateOfDis</td>
        </tr>
    }
</table>

输出:

localhost当前无法处理此请求。在ASP.NET控制器中检索来自dbContext的数据。

注意: 如果您想了解有关如何在视图中加载数据的更多详细信息,您可以在此处查看我们的官方文档

英文:

> This page isn't working - localhost is currently unable to handle this
> request. HTTP ERROR 500.

The information you have shared is not adequate regarding the error you are getting. Even not sure if you have any additional configuration on IIS or browser link or anything.

However, I found some inconsistency within your share code which may not function as expected.

Let's check how we can get th patient list and in order to do that, what part of your code need to improve.

Assuming you have following model:

public class Patients
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Status {get; set; }
        public DateTime DateOfAdm { get; set; }
        public DateTime DateOfDis { get; set; }
    }
  1. Incorrect action in controller:

You are trying to display list of patient within view but you are using IEnumerable&lt;Patients&gt; yet not returning view instead you are just doing return patients; which certainly wouldn't generate view html.

You should fix it as following:

public ActionResult GetAllPatients()
        {
            var patients = db.Patients.ToList();
            return View(patients);
        }

Incorrect model reference in view:

You are using this @model V._3._0.Models.PatientsData reference within your view but you are returning Patients.ToList() which should be type of Patients class. Thus, your reference should be :

@model IEnumerable&lt;YourProjectName.Models.Patients&gt;

Wrong foreeach loop invocation:

As your controller sending IEnumerable<Patients> and @model reference would contain that, you don't need to call Model.GetAllPatients() and you cannot, this is incorrct syntax.

Instead you should write as following:

@model IEnumerable&lt;DotNet6MVCWebApp.Models.Patients&gt;
&lt;table&gt;
    @foreach (var patients in Model)
    {
        &lt;tr&gt;
            &lt;td&gt;@patients.Id.&lt;/td&gt;
            &lt;td&gt;@patients.Name&lt;/td&gt;
            &lt;td&gt;@patients.Status&lt;/td&gt;
            &lt;td&gt;@patients.DateOfAdm&lt;/td&gt;
            &lt;td&gt;@patients.DateOfDis&lt;/td&gt;

        &lt;/tr&gt;
    }
&lt;/table&gt; 

Output:

localhost当前无法处理此请求。在ASP.NET控制器中检索来自dbContext的数据。

Note: If you would like to know more details on how can we load data in view you could check our official document here

huangapple
  • 本文由 发表于 2023年4月11日 02:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定