在加载页面时,我很难加载下拉列表的数据。我总是得到一个错误,我的对象是空的。当我调试时,似乎
PrepareViewBag()
函数在甚至执行中的代码之前引发
Globals.cs
意外地填写下拉列表。我不知道代码为什么会这样,但在我看来,它至少应该
PopulateDropDownList()
功能
全局.cs
在发出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");
}
}
System.Web.MVC.dll中发生“System.ArgumentNullException”类型的异常,但未在用户代码中处理。
附加信息:值不能为空。
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;
}
}
}
非常感谢