全局下拉列表始终为空

Global DropDown list allways null

我在加载页面时无法加载下拉列表数据。不知何故,我总是得到一个错误,说我的对象是空的。当我调试时,似乎 PrepareViewBag() 函数中的错误甚至在执行 Globals.cs 中的代码以实际填充下拉列表之前就出现了。我不知道为什么代码会这样,但在我看来,它至少应该在 Globals.cs 中的 PopulateDropDownList() 函数发出 ArgumentNullException 之前。有什么建议么?

调用视图:

public ActionResult Index()
{
    PreprareViewBag();
    return View();
}

准备ViewBag(异常):

public class BaseController : Controller 
{
    protected void PreprareViewBag()
    {
        ViewBag.DropDownListExtension = new SelectList(Globals.Constants.DropDownListExtension, "Value", "Text");
        ViewBag.DropDownListDeveloper = new SelectList(Globals.Constants.DropDownListDeveloper, "Value", "Text");
        ViewBag.DropDownListSchema = new SelectList(Globals.Constants.DropDownListSchema, "Value", "Text");
        ViewBag.DropDownListRelease = new SelectList(Globals.Constants.DropDownListRelease, "Value", "Text");
        ViewBag.DropDownListBuild = new SelectList(Globals.Constants.DropDownListBuild, "Value", "Text");
        ViewBag.DropDownListStatus = new SelectList(Globals.Constants.DropDownListStatus, "Value", "Text");
        ViewBag.DropDownListDeploymentRelease = new SelectList(Globals.Constants.DropDownListDeploymentRelease, "Value", "Text");
    }
}

An exception of type 'System.ArgumentNullException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: Value cannot be null.

Globals.cs 填充下拉列表:

public class Globals : Controller
{
    // GET: Globals
    public class Constants
    {
        public static String ConnectionString;

        //ADM Portal specific stuff
        public static List<SelectListItem> DropDownListRelease;
        public static List<SelectListItem> DropDownListBuild;
        public static List<SelectListItem> DropDownListSchema;
        public static List<SelectListItem> DropDownListStatus;
        public static List<SelectListItem> DropDownListDeveloper;
        public static List<SelectListItem> DropDownListExtension;
        public static List<SelectListItem> DropDownListDeploymentRelease;   //deployable releases




        public Constants()
        {      
            PopulateDropDownList(ref DropDownListRelease, "ADM_PORTAL.P_LISTRELEASES");
            PopulateDropDownList(ref DropDownListBuild, "ADM_PORTAL.P_LISTBUILDS");
            PopulateDropDownList(ref DropDownListSchema, "ADM_PORTAL.P_LISTSCHEMAS");
            PopulateDropDownList(ref DropDownListStatus, "ADM_PORTAL.P_LISTSTATUSES");
            PopulateDropDownList(ref DropDownListDeveloper, "ADM_PORTAL.P_LISTDEVELOPERS");
            PopulateDropDownList(ref DropDownListExtension, "ADM_PORTAL.P_LISTFILEEXTENSIONS");
            PopulateDropDownList(ref DropDownListDeploymentRelease, "ADM_PORTAL.P_LISTDEPLOYMENTRELEASES");
        }

        /// <summary>
        /// Populates drop down list from the database.
        /// </summary>
        /// <param name="destinationList">populates only if list is null</param>
        /// <param name="oracleCommand">e.g.: ADM_PORTAL.P_LISTFILEEXTENSIONS</param>
        /// <returns>number of items in the list</returns>
        public static int PopulateDropDownList(ref List<SelectListItem> destinationList, string oracleCommand)
        {
            ConnectionString = "User Id=adm_owner; password=/Bpvc7bm_wenYz#@; Data Source=chdbd1_genone.ch.glencore.net;";

            OracleParameter[] oracleParameters = new OracleParameter[2];
            oracleParameters[0] = new OracleParameter("pc_recordset", OracleDbType.RefCursor, ParameterDirection.Output);
            oracleParameters[1] = new OracleParameter("pn_includecanceled", OracleDbType.Long, 30, 0, ParameterDirection.Input);
            return PopulateDropDownList(ref destinationList, oracleCommand, oracleParameters);
        }

        public static int PopulateDropDownList(ref List<SelectListItem> destinationList, string oracleCommand, OracleParameter[] oracleParameters)
        {
            int i = 0;

            if (destinationList == null)
            {
                destinationList = new List<SelectListItem>();
                using (OracleConnection con = new OracleConnection(ConnectionString))
                {
                    con.Open();
                    OracleCommand cmd = new OracleCommand(oracleCommand, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.BindByName = true;
                    for (int p = 0; p < oracleParameters.Length; cmd.Parameters.Add(oracleParameters[p]), p++) ;

                    // Execute command
                    OracleDataReader reader;
                    try
                    {
                        reader = cmd.ExecuteReader();
                        if (!reader.HasRows)
                        {
                            //nothing on the list
                        }
                        else
                        {
                            //build list
                            while (reader.Read())
                            {
                                SelectListItem sli = new SelectListItem();
                                sli.Value = reader.GetString(1);        //DATAVALUE
                                sli.Text = reader.GetString(0);         //DISPLAYVALUE
                                destinationList.Add(sli);
                                i++;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // log error...
                    }
                }
            }
            else
            {
                i = destinationList.Count;
            }

            return i;
        }

    }
}

非常感谢

您引用的字段是静态的,但您的构造函数不是。因此,在访问静态 属性.

之前,构造函数中的代码不会 运行

改为静态构造函数,请参阅 MSDN: Static Constructors (C# Programming Guide):

改变

public Constants()

static Constants()