简体   繁体   English

如何传递C#中的列表对象中的值?

[英]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;
    }
}

如何通过列表<object>通过 MultipartFormDataContent c#<div id="text_translate"><p> 我试图了解如何使用 MultipartFormDataContent 进行 API 调用。 到目前为止,我已经能够发送带有字符串的变量和列表,但是每当我尝试发送带有整数的列表或带有类的列表时,API 都会检索到一个空列表。</p><p> 我该如何发送?</p><p> 这是我的有效代码:</p><pre> var multiForm = new MultipartFormDataContent(); var json = JsonConvert.SerializeObject(testClass.steps); //this is list of string with steps multiForm.Add(new StringContent(testEmployee.UserID.ToString()), "UserID"); multiForm.Add(new StringContent(json), "steps");</pre><p> 那么如何发送整数列表或员工列表? 我试过像上面那样做,但没有用。 然后 API 检索一个空列表。</p></div></object> - How to pass List<object> via MultipartFormDataContent c#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中的struct中传递值列表 - how to pass a list of values in struct in c# 传递 C# 中的值列表 - Pass a list of values in C# 如何在C#数据绑定中将值列表作为参数传递 - How to pass a list of values as parameter in c# databinding 如何使用C#将值传递到ActiveCampaign中的列表 - How to pass values to a list in ActiveCampaign using C# 如何在c#databinding中将值列表作为参数传递 - How to pass a list of values as parameter in c# databinding 如何将列表对象传递给 Web API PUT 方法 C# - How to Pass List object to Web API PUT method C# 如何将列表对象从 C# 传递到 Oracle 存储过程? - How To Pass a List Object From C# to an Oracle Stored Procedure? 如何将 Object 作为 c# controller 中的字符串列表传递? - How to pass Object in as string list in c# controller? 如何通过列表<object>通过 MultipartFormDataContent c#<div id="text_translate"><p> 我试图了解如何使用 MultipartFormDataContent 进行 API 调用。 到目前为止,我已经能够发送带有字符串的变量和列表,但是每当我尝试发送带有整数的列表或带有类的列表时,API 都会检索到一个空列表。</p><p> 我该如何发送?</p><p> 这是我的有效代码:</p><pre> var multiForm = new MultipartFormDataContent(); var json = JsonConvert.SerializeObject(testClass.steps); //this is list of string with steps multiForm.Add(new StringContent(testEmployee.UserID.ToString()), "UserID"); multiForm.Add(new StringContent(json), "steps");</pre><p> 那么如何发送整数列表或员工列表? 我试过像上面那样做,但没有用。 然后 API 检索一个空列表。</p></div></object> - How to pass List<object> via MultipartFormDataContent c# 如何在通用对象列表C#中获取通用对象的值 - how to get values of generic object in generic object list C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM