在方法C#MVC之间传递值

问题描述 投票:2回答:1

我有一个GetUser方法来调用sql过程并返回几个字段。我需要对值进行哈希处理,但是在测试中它可以工作。这是方法:

(edit:“ The_User”是我的User类在方法之外设置的公共实例。这就是我在试图使Person_ID传递到SaveRecord方法后最终得到的结果。这不同/更好)

 public ActionResult GetUser(Models.User model)
    {
        User x = new User();
        x.User_Name = model.User_Name;
        x.Password = model.Password;
        using (var conn = new SqlConnection("connection stuff"))
        using (var com = new SqlCommand("GetUser", conn)
        { CommandType = CommandType.StoredProcedure })
        {
            com.Parameters.AddWithValue("@User_Name", x.User_Name);
            com.Parameters.AddWithValue("@Password", x.Password);
            conn.Open();
            using (var reader = com.ExecuteReader())
            {
                while (reader.Read())
                {
                    The_User.User_Name = reader["User_Name"].ToString();
                    The_User.Password =  reader["Password"].ToString();
                    The_User.Person_ID = reader["PersonID"].ToString();
                }
                if (x.User_Name == The_User.User_Name && x.Password == The_User.Password)
                {
                    User_Auth = 1;
                }
                else
                {
                    User_Auth = 0;
                }
            }
            conn.Close();
        }
    }

稍后在工作流程中,我想从GetUser方法获取Person_ID值并将其传递给另一个方法-“保存记录”。这里是:

 public ActionResult SaveRecord(Models.ModelData model)
    {
        ModelData md = new ModelData();
        md.thing1 = model.thing1;
        md.thing2 = model.thing2;
        md.thing3 = model.thing3;

        if (md.thing1 is null) { md.thing1 = ""; };
        if (md.thing2 is null) { md.thing2 = ""; };
        if (md.thing3 is null) { md.thing3 = ""; };


        using (var conn = new SqlConnection("connection stuff"))
        using (var com = new SqlCommand("GetAppData1", conn)
        { CommandType = CommandType.StoredProcedure })
        {
            com.Parameters.AddWithValue("@thing1", md.thing1);
            com.Parameters.AddWithValue("@thing2", md.thing2);
            com.Parameters.AddWithValue("@thing3", md.thing3);
            com.Parameters.AddWithValue("@PID", I NEED PERSON ID RIGHT HERE!!!);
            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();
        }
        return View();
    }

我已经尝试过在Stack溢出中找到的许多解决方案,并在调用Save Record方法时需要的时候一直运行到我传递的变量为null的位置。我感谢所有帮助! :)

c# asp.net-mvc methods parameter-passing
1个回答
0
投票

有多种方法可以做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.