如何在MVC的下拉列表中绑定动态值 [英] How to bind dyamic values in dropdown in MVC

查看:64
本文介绍了如何在MVC的下拉列表中绑定动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下载表单(VIEW),其文本和值我从数据库绑定,我想在表单的提交按钮中保存数据库中所选的drodown值。







下拉值和文本在下拉列表中绑定,但是在提交按钮上

ObjTournamentRegistration.COUNTRY_ID为空



我尝试过:



调用视图的动作方法

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

推荐答案

COUNTRY_NAME是你控制的价值,为什么你读COUNTRY_ID?您可以在调试器中查看整个帖子以查看正在发送的内容。如果浏览器正在向您发送数据,请首先解决,然后您将知道您的错误所在
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


这篇关于如何在MVC的下拉列表中绑定动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆