将值附加到viewmodel中的类列表

时间:2014-05-28 00:21:08

标签: asp.net-mvc-4 c#-4.0

我需要将值附加到类别列表中。

我尝试了几种组合,包括当时追加1行或附加范围。

marticle.ListOfCategories.Add(crow); THiS DID NOT WORK
  // marticle.ListOfCategories(ccarticle); THiS DID NOT WORK

我收到以下错误:

  

对象引用未设置为对象的实例。

这是我的类别类:

public class Category
{
    public int ID { get; set; }

    [Required]
    public string Title { get; set; }
}

这是我的观点模型

public class ArticleEditViewModel
    {
        public int ID { get; set; }

        [Required]
        public Boolean Active { get; set; }

        [Required]
        public string Title { get; set; }

        public List<Category> ListOfCategories { get; set; }
    }

这是我的记录集追加复数

 public ArticleEditViewModel ArticleSelectById(int? id)
        {
            ArticleEditViewModel marticle = new ArticleEditViewModel();
            string connString =
                System.Configuration.ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "content.Article_Select_ById";
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            if (rdr["ID"] != DBNull.Value)
                            {
                                marticle.ID = Convert.ToInt32(rdr["ID"]);
                            }
                            if (rdr["Title"] != DBNull.Value)
                            {
                                marticle.Title = rdr["Title"].ToString();
                            }                            
                        }
                    }
                }
                conn.Close();

                //Append the list of categories

                 List<Category> ccarticle = new List<Category>();
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "dbo.Category_SelectAllActive";

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            Category crow = new Category();
                            if (rdr["ID"] != DBNull.Value)
                            {
                                crow.ID= Convert.ToInt32(rdr["ID"]);
                            }
                            if (rdr["Description"] != DBNull.Value)
                            {
                                crow.Description = rdr["Description"].ToString();
                            }
                            crow.Active = true;
                            ccarticle.Add(crow);
                            marticle.ListOfCategories.Add(crow); //THiS DID NOT WORK
                        }
                    }
                }
               // marticle.ListOfCategories(ccarticle); THiS DID NOT WORK
            }
            return marticle;
        }

1 个答案:

答案 0 :(得分:2)

你没有显示定义为什么乌鸦。这是添加它的简便方法

marticle.ListOfCategories.Add(new Category() { ID = YourID, Title = YourTitle });

只需用数据库值替换yourid和yourtitle。由于它是Class的列表,因此您需要定义该类型的对象并将其添加到列表中。

编辑:

在设置列表之前,您需要初始化它

marticle.ListOfCategories = new List<Category>();

我建议在你班级的构造函数中设置它

public ArticleEditViewModel(){
    ListOfCategories = new List<Category>();
}

这样,每次创建viewmodel的新实例时,列表都会自动初始化

EDIT2:

构造函数就像这样进入类

public class ArticleEditViewModel
{
    public ArticleEditViewModel(){
        ListOfCategories = new List<Category>();
    }
    public int ID { get; set; }

    [Required]
    public Boolean Active { get; set; }

    [Required]
    public string Title { get; set; }

    public List<Category> ListOfCategories { get; set; }
}