我正在努力在加载页面时加载下拉数据。我总是得到一个错误,我的对象是null。当我调试时,似乎PrepareViewBag()
函数中的错误在执行Globals.cs
中的代码之前就会提升,以实际填充下拉列表。我不知道为什么代码的行为是这样的,但在我看来,它应该至少在发出ArgumentNullException之前来到PopulateDropDownList()
中的Globals.cs
函数。有什么建议?
调用视图:
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");
}
}
类型' System.ArgumentNullException'的异常发生在System.Web.Mvc.dll中但未在用户代码中处理
附加信息:值不能为空。
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;
}
}
}
非常感谢
答案 0 :(得分:1)
您引用的字段是静态的,但您的构造函数不是。因此,在访问静态属性之前,构造函数中的代码不会运行。
使构造函数变为静态,请参阅MSDN: Static Constructors (C# Programming Guide):
更改
public Constants()
要
static Constants()