从数据库获取数据并进行分配

时间:2015-06-20 13:47:33

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

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

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

代码(SystemManager):

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也是qrc数据库中的1}}不是0.

1 个答案:

答案 0 :(得分:0)

您的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");
        }
    }