Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having dropdown in a form(VIEW) whose text and values i am binding from database,i want to save the selected value of drodown in database on submit button of form.



dropdown values and text are binding in dropdown , but on submit button
"ObjTournamentRegistration.COUNTRY_ID" is null

What I have tried:

action method for calling view
public ActionResult TOURNAMENT_PROFILE()
        {
            objSQLHeplper = new SQLHelper();
            objCommonClass = new CommonClass();
            HttpCookie cookie = Request.Cookies["userInfo"];
            ObjTournamentDetail = new TournamentRegistration();
            
            if (Request.Cookies["UserName"] != null && Request.Cookies["UserPassword"] != null )
            {
                UserName = Request.Cookies["UserName"].Value.ToString();
                Password = Request.Cookies["UserPassword"].Value.ToString();
                ObjMenuName = objCommonClass.UserLoin(UserName, Password);
                if (ObjMenuName != null)
                {
                  
                    ObjMenuName = null;
                    dt= new DataTable();
                    dt = objCommonClass.GETCOUNTRY();
                    List<SelectListItem> OblListTournamentRegistration = new List<SelectListItem>();
                    foreach(DataRow dr in dt.Rows)
                    {
                        OblListTournamentRegistration.Add(new SelectListItem() { Text = dr["COUNTRY_NAME"].ToString(), Value = dr["COUNTRY_ID"].ToString() });
       
                    }
                    ViewBag.Country = OblListTournamentRegistration;

                    

                    if (Session["ErrorMessage"] != null)
                    {
                        ViewBag.ALertMessage = Session["ErrorMessage"].ToString();
                        ViewBag.JavaScriptFunction = string.Format("FailMessagePopup('{0}');", AlertMessage);
   
                    }
                    else if (Session["SuceessMessage"] != null)
                    {
                        ViewBag.ALertMessage = Session["SuceessMessage"].ToString();
                        ViewBag.JavaScriptFunction = string.Format("SuccessMessagePopup('{0}');", AlertMessage);
                    }
                    return View("TournamentRegistration");
                }
                else
                {
                    return RedirectToAction("MainPage", "Home");
                }
            }
            else
            {
                return RedirectToAction("MainPage", "Home");
            }
        }



view
@Html.DropDownListFor(model => model.COUNTRY_NAME, new SelectList(ViewBag.Country, "Value", "Text"), new { @class = "form-control glyphicon glyphicon-user", @value = "", placeholder = "Tournament Name", id = "txtTournamentName" })


action method on submit button
public ActionResult TournamentRegistration(TournamentRegistration ObjTournamentRegistration)
        {
            
           try
            {

               
                MySqlConnection connection = new MySqlConnection(SQLHelper.ConnectionString);
                connection.Open();
                MySqlCommand cmd = new MySqlCommand("TOURNAMENT_REGISTRATION", connection);
                cmd.CommandType = CommandType.StoredProcedure;
	        cmd.Parameters.AddWithValue(
                cmd.Parameters.AddWithValue("@PI_COUNTRY", ObjTournamentRegistration.COUNTRY_ID);
                .ToString());
                cmd.ExecuteNonQuery();               
                TournamentLogo=null;   
                Session["SuceessMessage"] = "Sucessfull Registered";
                
                return RedirectToAction("TOURNAMENT_PROFILE", "MainPageMenuBar");
            }
            catch (Exception ex)
            {
                Session["ErrorMessage"] = "Something Went Wrong";
                return RedirectToAction("TOURNAMENT_PROFILE", "MainPageMenuBar");
            }
        }
}
Posted
Updated 2-May-19 11:57am
Comments
Richard Deeming 9-May-19 15:05pm    
Request.Cookies["UserPassword"]


No, no, no, no, no, NO!!!!

NEVER store the user's credentials in cookies.

You validate the credentials once, and generate a cryptographically secure authentication token. You store the authentication token in a cookie, marked as HTTP-only, preferably marked as "secure" and "same-site". On subsequent requests, you extract the authentication token and validate that.

You also make sure that you never store the user's password in plain-text, or using a reversible encryption. You only ever store a salted hash of the password, using a unique salt per record, and using multiple rounds of a key-derivation function like PBKDF2 or bcrypt.

But you don't need to do any of that yourself. ASP.NET already has perfectly good authentication and authorisation systems built-in. Use those instead.

Security, Authentication, and Authorization with ASP.NET MVC[^]
ASP.NET Identity[^]

1 solution

COUNTRY_NAME is the value of your control, why are you reading COUNTRY_ID? You can view the entire post in the debugger to see what IS being sent. Work out first if the browser is sending you the data, then you'll know where you bug lies
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900