Trevor Staub
Trevor Staub

Reputation: 23

Pass values between methods C# MVC

I have a GetUser method to call a sql procedure and return a couple of fields. I need to hash the values but in testing it is working. Here is the method:

(edit: "The_User" is a public instance of my User class set outside of the method. This is just what I ended up with after screwing around trying to get the Person_ID to pass to my SaveRecord Method. I am open to doing this differently/better)

 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();
        }
    }

Later on down the workflow I want to get the Person_ID value from the GetUser method and pass it to another method - "Save Record". Here it is:

 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();
    }

I have tried numerous solutions found on Stack overflow and keep running into my passed variable being null by the time I need it when the Save Record method is called. I appreciate any and all Help! :)

Upvotes: 2

Views: 191

Answers (1)

salli
salli

Reputation: 782

There are multiple ways to do this.

First, try adding @Html.HiddenFor(m=> m.Person_ID") as part of the View.cshtml being returned by GetUser ActionResult. Assuming you are returning return View("Name Of View", The_User).

If you are using User.Identity you can extend it as described by the answer on this post

Upvotes: 1

Related Questions