【问题标题】:Getting data from the database and assign it从数据库中获取数据并赋值
【发布时间】:2015-06-20 13:47:32
【问题描述】:

我已经分配了从数据库中检索到的数据(ChancesLeft),并将其分配给创建的变量。但是当我检查控制器中创建的变量(已经分配给从数据库中检索到的数据(ChancesLeft)时,创建的变量的值始终为 0,即使我已经调用了该检索函数。但是,当我检查在检索函数所在的类中,创建的变量的值不是0,而是跟在检索到的数据的值之后(ChancesLeft)。

我的问题是,如何将值从类传递给控制器​​?这样我就可以用了?

代码(系统管理器):

public void RetrieveChancesLeft(string Name)
        {
            MyModel context = new MyModel();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                string query = "SELECT [Name], [ChancesLeft] FROM [Credentials] WHERE [Name] = @Name";

                conn.Open();

                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
                    cmd.Parameters["@Name"].Value = Name;

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            context.Chances = Convert.ToInt32(reader["ChancesLeft"]);
                        }
                    }
                }

                conn.Close();
            }
        }

控制器:

 SystemManager _sm = new SystemManager();

 [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

 [HttpPost]
    public ActionResult Index(MyModel context, string Name)
    {
        _sm.RetrieveChancesLeft(Name);

        if (context.Chances > 0)
        {
            return RedirectToAction("About", "Home");
        }

        else
        {
            return RedirectToAction("GetStarted", "Home");
        }
    }

问题是,每当我运行程序并输入Name 时,它总是重定向到GetStarted 视图,这意味着context.Chances 始终为0,即使数据库中的ChancesLeft 不是0.

【问题讨论】:

    标签: c# sql asp.net asp.net-mvc database


    【解决方案1】:

    您的 RetrieveChancesLeft 函数应将本地上下文对象存储在 SystemManager 对象中以供检索或将上下文返回给调用者。您可以更改 RetrieveChancesLeft 函数的返回类型以将对象传递给控制器​​:

    public MyModel RetrieveChancesLeft(string Name)
        {
            MyModel context = new MyModel();
    
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                string query = "SELECT [Name], [ChancesLeft] FROM [Credentials] WHERE [Name] = @Name";
    
                conn.Open();
    
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
                    cmd.Parameters["@Name"].Value = Name;
    
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            context.Chances = Convert.ToInt32(reader["ChancesLeft"]);
                        }
                    }
                }
    
                conn.Close();
    
            }
            return context;
    
        }
    

    还有你的控制器:

        SystemManager _sm = new SystemManager();
    
     [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
    
     [HttpPost]
        public ActionResult Index(MyModel context, string Name)
        {
            context = _sm.RetrieveChancesLeft(Name);
    
            if (context.Chances > 0)
            {
                return RedirectToAction("About", "Home");
            }
    
            else
            {
                return RedirectToAction("GetStarted", "Home");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 2023-01-20
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 2017-08-29
      • 1970-01-01
      相关资源
      最近更新 更多