Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

asp.net - Passing lists to the view .Net Core

Im looping through all the results from the SQL query in a .Net Core project. here is Model

    public class Mymessagesinfo
{
    public int MyMessagesCount { get; set; }

    public List<int> MessagesIDs { get; set; }
    public List<int> MessagesSendersid { get; set; }
    public List<string> MessagesSenders { get; set; }
    public List<string> MessagesTitles { get; set; }
    public List<string> MessagesInformation { get; set; }

    public List<string> MessagesStatus { get; set; }

}

I loop through the users messages in my controller then i pass that model to the view

sqlcon.Open();
        int? userid = HttpContext.Session.GetInt32("UserID");
        SqlCommand sqlcom = new SqlCommand("select * from messages where Messagereceiver=" +userid , sqlcon);
        SqlDataReader reader = sqlcom.ExecuteReader();
        if(reader.HasRows)
        {
            
            int index = 0;
            while(reader.Read())
            {
                string s;
                s = reader[0].ToString();

                Mymessages.MessagesIDs.Add(int.Parse(s));
                Mymessages.MessagesSendersid.Add(int.Parse(reader[1].ToString()));
                Mymessages.MessagesTitles.Add(reader[3].ToString());
                Mymessages.MessagesInformation.Add(reader[4].ToString());
                Mymessages.MessagesStatus.Add(reader[5].ToString());
                index++;
            }
            Mymessages.MyMessagesCount = index;

        }

the very first line Mymessages.MessagesIDs.Add(int.Parse(s)); it throws an exception saying System.NullReferenceException: 'Object reference not set to an instance of an object i wanted to make sure that reader was holding the results so i added int s and checked on it and it was holding the value it was supposed to. whats going wrong here? is this how we are supposed to pass list-like data to the view?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to initlize MessagesIDs in entity Mymessages, like this:

var Mymessages = new Mymessagesinfo()
{
    MessagesIDs = new List<int>()
};

Mymessages.MessagesIDs.Add(id);

Or just define the class like this,

public class Mymessagesinfo
{
    public int MyMessagesCount { get; set; }

    public List<int> MessagesIDs { get; set; } = new List<int>();
    public List<int> MessagesSendersid { get; set; } = new List<int>();
    public List<string> MessagesSenders { get; set; } = new List<string>();
    public List<string> MessagesTitles { get; set; } = new List<string>();
    public List<string> MessagesInformation { get; set; } = new List<string>();

    public List<string> MessagesStatus { get; set; } = new List<string>();

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...