Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将项目添加到列表中_C#_Asp.net_.net_Asp.net Mvc - Fatal编程技术网

C# 无法将项目添加到列表中

C# 无法将项目添加到列表中,c#,asp.net,.net,asp.net-mvc,C#,Asp.net,.net,Asp.net Mvc,我在将项目添加到列表时遇到问题,以下是我的代码: using (var poolDbContext = new PoolContext()) { Question Q = new Question(); Q.Text = "O triunfo do FC Porto frente ao Benfica arrumou de vez as contas do título?"; Q.Answer.Add ="Sim, o FC Porto vai ser campeão."

我在将项目添加到列表时遇到问题,以下是我的代码:

using (var poolDbContext = new PoolContext())
{
    Question Q = new Question();
    Q.Text = "O triunfo do FC Porto frente ao Benfica arrumou de vez as contas do título?";
    Q.Answer.Add ="Sim, o FC Porto vai ser campeão.");


    Q.Answer.Add('Sim, o FC Porto vai ser campeão');
    Q.Answer.Add("Não, o Benfica ainda tem uma palavra a dizer.");
    Q.Answer.Add("Não, o Sporting ainda vai ser campeão.");
    Q.Answer.Add("Não ligo a futebol.");
    poolDbContext.Questions.Add(Q);
    poolDbContext.Answers.Add(Q);
    var count = poolDbContext.SaveChanges();
}
这是我的.模型.问题:

public class Question
{
    public string Text { get; set; }
    public List<Answer> Answer { get; set; }

}

你需要像这样的东西

        Q.Answer.Add(new Answer("Sim, o FC Porto vai ser campeão"));
在这种情况下,Answer需要一个带字符串的构造函数,或者将Answer设置为字符串。对于字符串,也使用“”而不是“”//这些用于字符

编辑: 你的答案应该是这样的:

public class Answer
{
    public string Questions { get; set; }
    public string Answers { get; set; }
    public string Results { get; set; }

    public Answer(string text)
    {
         Answers = text;
    }

    public Answer(string question, string answer, string results)
    {
         Questions = question;
         Answers = answer;
         Results = results;
    }
}

在构造函数中初始化答案列表

public class Question
{
    public string Text {get; set; }
    public List<Answer> Answers {get; set; } 


     public Question(){
     Answers=new List<Answer>();
    }
}
公开课问题
{
公共字符串文本{get;set;}
公共列表答案{get;set;}
公众问题({
答案=新列表();
}
}
添加像这样的新项目

Add(新答案{propert1=value1,property2=value2})

在这一行中:

Q.Answer.Add('Sim, o FC Porto vai ser campeão');
…代码认为您正在尝试添加答案,但实际上您给了它一个字符串文本

如果答案是字符串,我建议进行以下更改:

public class Question
{
    public string Text { get; set; }
    public List<string> Answer { get; set; }  //<-- just store strings
}
或者,如果您想变得特别聪明,可以设置隐式转换:

public class Answer 
{ 
    public string Answers { get; set; } 

    static public implicit operator Answer(string input)
    {
        var a = new Answer();
        a.Answers = input;
        return a;
    }
} 
添加隐式转换运算符后,现在可以再次使用:

Q.Answer.Add("Sim, o FC Porto vai ser campeão");

昨天我一个人成功了,正确的方法是

        using (var poolDbContext = new PoolContext())
        {
            if (poolDbContext.Questions.Count() == 0)
            {
                Question Q = new Question();

                Q.Text = "O triunfo do FC Porto frente ao Benfica arrumou de vez as contas do título?";

                Q.Answers.Add(new Answer { Answers = "Sim, o FC Porto vai ser campeão." });
                Q.Answers.Add(new Answer { Answers = "Não, o Benfica ainda tem uma palavra a dizer." });
                Q.Answers.Add(new Answer { Answers = "Não, o Sporting ainda vai ser campeão." });
               Q.Answers.Add(new Answer { Answers = "Não ligo a futebol." });
                poolDbContext.Questions.Add(Q);
                var count = poolDbContext.SaveChanges();
            }
        }
        return View();

您的
Answer
属性是
List
-您需要向其中添加
Answer
的实例-
Q.Answer.add(新答案{….})我可能对
C#
不够熟悉,但是在
poolDbContext.Answers.Add中(答案=“Não,o Benfica ainda tem uma palavra a dizer.”)例如,“==”看起来很奇怪,这不是一个表达式吗?显示你的
答案
class@SlavenHvar使用制度;使用System.Collections.Generic;使用系统文本;namespace PoolManager.Models{public class Answer{public string Questions{get;set;}public string Answers{get;set;}public string Results{get;set;}}}}@dorinmuntanu和类似“Sim,o FC Porto vai ser campeão”的文本应该放在Answer类的哪个属性中?或者Answer可以有一个公共属性,我们称之为
Text
,您可以像这样使用属性初始化:
new Answer{Text=“Sim,o FC…”
“Answer”不包含带1个参数的构造函数,这是我收到的信息getting@dorinmunteanu那么,是什么阻止您简单地添加这样一个构造函数呢?这是你自己的密码,不是吗?
Q.Answer.Add("Sim, o FC Porto vai ser campeão");
        using (var poolDbContext = new PoolContext())
        {
            if (poolDbContext.Questions.Count() == 0)
            {
                Question Q = new Question();

                Q.Text = "O triunfo do FC Porto frente ao Benfica arrumou de vez as contas do título?";

                Q.Answers.Add(new Answer { Answers = "Sim, o FC Porto vai ser campeão." });
                Q.Answers.Add(new Answer { Answers = "Não, o Benfica ainda tem uma palavra a dizer." });
                Q.Answers.Add(new Answer { Answers = "Não, o Sporting ainda vai ser campeão." });
               Q.Answers.Add(new Answer { Answers = "Não ligo a futebol." });
                poolDbContext.Questions.Add(Q);
                var count = poolDbContext.SaveChanges();
            }
        }
        return View();