[英]Repo method not returning data to controller (ASP.NET)
I have repo method to get data from db 我有回购方法从数据库获取数据
Here is repo method 这是回购方法
public List<PatientMasterDataViewModel> GetPatientData(string name)
{
using (var ctx = new ApplicationDbContext())
{
List<PatientMasterDataViewModel> patientdata = new List<PatientMasterDataViewModel>();
var items = ctx.Patients.Where(x => x.Name.Contains(name)).ToList();
for (int i = 1; i < items.Count; i++)
{
patientdata.Add(new PatientMasterDataViewModel
{
Id = items[i].Id,
Name = items[i].Name,
Birthday = items[i].Date_of_Birthday
});
}
return patientdata;
}
}
after this I need to get this data in controller method 之后,我需要在控制器方法中获取此数据
Here is controller method 这是控制器方法
public JsonResult PatientMasterDataInfo(string name)
{
var masterdatainfo = _repository.GetPatientData(name);
return Json(masterdatainfo.ToArray(), JsonRequestBehavior.AllowGet);
}
In Repo in items
I have data 在回购的
items
我有数据
Here is screen 这是屏幕
enter image description here 在此处输入图片说明
But for some reasons it not adding to patientdata and as result in controller I have null. 但是由于某些原因,它没有添加到患者数据中,导致控制器我为空。
Where is my trouble? 我的麻烦在哪里?
You have loop started from 1. Please start it from 0 index. 您的循环从1开始。请从0索引开始。
public List<PatientMasterDataViewModel> GetPatientData(string name)
{
using (var ctx = new ApplicationDbContext())
{
List<PatientMasterDataViewModel> patientdata = new List<PatientMasterDataViewModel>();
var items = ctx.Patients.Where(x => x.Name.Contains(name)).ToList();
for (int i = 0; i < items.Count; i++)
{
patientdata.Add(new PatientMasterDataViewModel
{
Id = items[i].Id,
Name = items[i].Name,
Birthday = items[i].Date_of_Birthday
});
}
return patientdata;
}
}
It should not be null
as per your code what you have but i see one issue in your code which is your loop is string from 1 not 0 so change it to : 根据您的代码,它不应该为
null
,但是我在您的代码中看到一个问题,这是您的循环是从1到0的字符串,因此将其更改为:
for (int i = 0; i < items.Count; i++)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.