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