为什么 viewmodel 无法从数据库中获取数据?即使它 return 值

why viewmodel can't get data from database?even though it return values

我有一个视图模型 class Paymentcart 并且在其中我创建了另外两个 classes paymentcart 的对象。

Class Paymentcart

namespace Temple.Models
{
    public class paymentcart
    {
        public cart cart { get; set; }
        public payment payment { get; set; }
    }
}

Class 购物车:

public class cart
{
         public long cid { get; set; }
         public long vid { get; set; }
         public long userid { get; set; }
         public long count { get; set; }
         public long tempid { get; set; }
         public string  name { get; set; }
         public string star { get; set; }
         public string dates { get; set; }
         public string vname { get; set; }
         public string vrate { get; set; }
         public string totalamount { get; set; }
         public int rows { get; set; }
}

Class付款:

public class payment
{
        public long cid { get; set; }
        public long vid { get; set; }
        public long userid { get; set; }

        public long tempid { get; set; }
        public string amt { get; set; }
        public string cname { get; set; }
        public long number { get; set; }
        public long securitycode { get; set; }
        public string expdate { get; set; }
        public string totalamount { get; set; }
}

但是当我从数据库中输入值时,它显示 "Null Exception" 错误(但数据库显示 return 值)。

这是控制器代码:

 List<paymentcart> qlist = new List<paymentcart>();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("getcart", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                      var q = new paymentcart();
                    q.cart = new cart();
                    cmd.Parameters.AddWithValue("@uid", uid);
                    con.Open();
                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                         q.cart.tempid = Convert.ToInt64(rd["TdId"]);
                          q.cart.userid  = Convert.ToInt32(rd["UserID"]);
                           q.cart.cid = Convert.ToInt32(rd["cid"]);
                           q.cart. vid = Convert.ToInt32(rd["v_id"]);
                            q.cart.name = Convert.ToString(rd["name"]);
                           q.cart. star = Convert.ToString(rd["star"]);
                           q.cart. dates = Convert.ToString(rd["dates"]);
                            q.cart.vname = Convert.ToString(rd["vname"]);
                           q.cart. vrate = Convert.ToString(rd["vrate"]);
                           qlist.Add(q);

                    }

                }

            }
            return qlist;

我在这里附上了截图:Error Page

我不知道我使用的方法是否正确;请帮我解决这个问题。

您创建了 paymentcart 的实例,这是正确的,但您错过了创建 paymentcart 的实例 cartclass,在实例化paymentcartclass对象的同时,创建cartclass

的对象

类似

 var q = new paymentcart();
 q.cart = new Cart(); //This will resolve error related to object instantiation
 //^^^ This was missing, I used naming conventions to differentiate between class name and object name 
 //Your code goes here
 cmd.Parameters.AddWithValue("@uid", uid);
 con.Open();
 SqlDataReader rd = cmd.ExecuteReader();

Bonus : Always follow naming conventions to declare class name, property name and variable names

  • 您的 class 姓名应以大写字母开头,即 PascalCase
  • 您的变量名应以小写字母开头,即 camelCase

请尝试以下代码:

public List<paymentcart> ViewCart(int uid)
        {
            List<paymentcart> qlist = new List<paymentcart>();
            using (SqlConnection con = new SqlConnection(connectionString))
            {

                using (SqlCommand cmd = new SqlCommand("getcart", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@uid", uid);
                    con.Open();
                    SqlDataReader rd = cmd.ExecuteReader();

                    while (rd.Read())
                    {
                        var q = new paymentcart();
                        q.cart = new cart();

                         q.cart.tempid = Convert.ToInt64(rd["TdId"]);
                          q.cart.userid  = Convert.ToInt32(rd["UserID"]);
                           q.cart.cid = Convert.ToInt32(rd["cid"]);
                           q.cart. vid = Convert.ToInt32(rd["v_id"]);
                            q.cart.name = Convert.ToString(rd["name"]);
                           q.cart. star = Convert.ToString(rd["star"]);
                           q.cart. dates = Convert.ToString(rd["dates"]);
                            q.cart.vname = Convert.ToString(rd["vname"]);
                           q.cart. vrate = Convert.ToString(rd["vrate"]);
                           qlist.Add(q);




                    }

                }

            }
            return qlist;
        }