如何更改此操作以从数据库而不是API检索数据 我想从SQL中的数据库中检索数据并在html输入元素中显示>>>>
public JsonResult getFromApi(string n)
{
ApInvoker inv = new ApInvoker(n);
var student = inv.getStd();
if (student == null)
{
return Json(new { error = "لا يمكن الوصول للخادم , تحقق من اتصال الانترنت" });
}
StudentDTO sm = new StudentDTO();
if (student.id == 0)
{
return Json(new { error = "لم يتم العثور على البيانات" });
}
sm.NID = Convert.ToString(student.nId);
sm.FristName = student.name.Split(' ')[0];
sm.FatherName = student.name.Split(' ')[1];
sm.GrandFatherName = student.name.Split(' ')[2];
sm.SureName = student.name.Split(' ')[3];
sm.DateOfBirth = student.birthDate;
sm.Address = student.address;
sm.PlaeceOfBirth = student.plcOBirth;
return Json(sm);
}
答案 0 :(得分:0)
在控制器中:
确保您在模型中创建并继承了DbContext,还添加了您的Dbset
表格。
vue-router
视图中: 首次导入模型 @model ProjectName.Models.TableName //例如Demo.Models.StudentMaster
public ActionResult getFromDataBase()
{
// create object for your context of Database
var studentContext = new studentContext();
// return tablename data you want to retrieve in view
return View(studentContext.TableName.ToList());
}