英文:
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<Patients> GetAllPatients()
{
var patients = db.Patients.ToList();
return patients;
}
This is my dbContext
:
public class DataContext : DbContext
{
//private readonly IConfiguration Configuration;
public DataContext(DbContextOptions<DataContext> options)
: base(options)
{
}
public DbSet<Patients> Patients { get; set; }
}
This is my view
@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 unable to spot where I am going wrong ...
答案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; }
}
- 控制器中的不正确操作:
您试图在视图中显示患者列表,但您使用的是IEnumerable<Patients>
,而没有返回视图,而只是使用return patients;
,这肯定不会生成视图HTML。
您应该将其修复如下:
public ActionResult GetAllPatients()
{
var patients = db.Patients.ToList();
return View(patients);
}
- 视图中的错误模型引用:
您在视图中使用了@model V._3._0.Models.PatientsData
的引用,但您返回的是Patients.ToList()
,应该是Patients类的类型。因此,您的引用应该如下:
@model IEnumerable<YourProjectName.Models.Patients>
- 错误的foreach循环调用:
由于您的控制器发送IEnumerable<Patients>
,而@model
引用将包含它,所以您不需要调用Model.GetAllPatients()
,也不能这样做,这是不正确的语法。
相反,您应该按照以下方式编写:
@model IEnumerable<DotNet6MVCWebApp.Models.Patients>
<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>
输出:
注意: 如果您想了解有关如何在视图中加载数据的更多详细信息,您可以在此处查看我们的官方文档。
英文:
> 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; }
}
- Incorrect action in controller:
You are trying to display list of patient within view but you are using IEnumerable<Patients>
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<YourProjectName.Models.Patients>
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<DotNet6MVCWebApp.Models.Patients>
<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>
Output:
Note: If you would like to know more details on how can we load data in view you could check our official document here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论