[英]How to pass values that are in a List Object in C#?
This maybe a simple one. 这也许很简单。 That is I have a some JSON details which comes as a list and then it converts to object.
那就是我有一些JSON详细信息,它以列表的形式出现,然后转换为对象。 So now the object contains 2 values since it is a list.
所以现在该对象包含2个值,因为它是一个列表。 So I wanted to pass them into the database.
所以我想将它们传递到数据库中。 How do I achieve this.
我该如何实现。
Here's what I tried. 这是我尝试过的。
public IHttpActionResult PostRegister([FromBody] dynamic register)
{
try
{
Newtonsoft.Json.Linq.JArray qualificationRes = (Newtonsoft.Json.Linq.JArray)register.qualification;
var qualification = qualificationRes.ToObject<List<Qualification>>(); //conversion to the List object
var sta2 = QMgrt.InsertQualifications(qualification,nid); //passing of List value.
return Ok("success");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return Ok("fail");
}
}
This is how I trying to insert it to the database 这就是我试图将其插入数据库的方式
public bool InsertQualifications(List<Qualification> quali,int newid) //Getting the List values
{
foreach (Qualification q in quali) // This is where the issues comes only one set of object is passed to the database
{
try
{
var status = db.AddQualiDetails("Insert_QualiDetails", q.Description, q.University, q.Date_of_award, q.Qauli_id, q.Quali_type, newid, q.Duration);
return true;
}
catch (Exception)
{
return false;
}
}
return true;
}
For more reference I have added this screenshot 有关更多参考,我添加了此屏幕截图
As seen in the screenshot there are two set of object. 如屏幕截图所示,有两组对象。 How do I pass all of them into the database.
如何将所有这些都传递到数据库中。 Help would be really appreciated
帮助将不胜感激
I don't know if I understand you right but maybe this could be the problem: 我不知道我是否理解正确,但这可能是问题所在:
public bool InsertQualifications(List<Qualification> quali,int newid) //Getting the List values
{
foreach (Qualification q in quali) // This is where the issues comes only one set of object is passed to the database
{
try
{
var status = db.AddQualiDetails("Insert_QualiDetails", q.Description, q.University, q.Date_of_award, q.Qauli_id, q.Quali_type, newid, q.Duration);
return true; <<<<<-------
}
catch (Exception)
{
return false;
}
}
return true;
}
You insert one List entry into your database and the return true;
您将一个List条目插入数据库,并
return true;
否则, return true;
When you use return
you exit the method and stop the foreach
. 使用
return
,退出方法并停止foreach
。 Regarding to this, your code is adding only one entry of your list. 关于此,您的代码仅添加列表的一个条目。 Try to just remove this
return true;
尝试删除此
return true;
Also check any exception occurred during the insertion. 还要检查插入期间发生的任何异常。
As far as i see you didn't call db.SaveChanges()
. 据我所知,您没有调用
db.SaveChanges()
。 SaveChanges
will commit your changes to database. SaveChanges
会将您的更改提交到数据库。 Also there is return true;
也有
return true;
in your loop, it breaks the loop. 在您的循环中,它打破了循环。
Here is correct code to save your data to database: 这是将数据保存到数据库的正确代码:
public bool InsertQualifications(List<Qualification> quali,int newid) //Getting the List values
{
foreach (Qualification q in quali) // This is where the issues comes only one set of object is passed to the database
{
try
{
var status = db.AddQualiDetails("Insert_QualiDetails", q.Description, q.University, q.Date_of_award, q.Qauli_id, q.Quali_type, newid, q.Duration);
}
catch (Exception)
{
return false;
}
}
db.SaveChanges();
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.