I have a project I am working on in MS VS 2012. I have a class that pulls data down from a database and puts it into an array of classes. That part of the code works seamlessly. The part I am having issue with is getting the data to the view for display. It seems that from the controller you should have the ability to pass classes through but I have not found it.
In the model
public Guid BoxGUID {get;set;}
[Required(ErrorMessage="A box number is required")]
public int BoxID { get; set; }
public DateTime Date { get; set; }
[Required(ErrorMessage="Please pick an option")]
public string BCAppearance { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string BCTrash { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string BCPad { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string BCClean { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string BCStrap { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string BCDoc { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string BCMaint { get; set; }
public string BCMaintNote { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string UBWrap { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string UBPiece { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string NPSec { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string NPDmg { get; set; }
[Required(ErrorMessage = "Please pick an option")]
public string NPHardware { get; set; }
public string TruckNum { get; set; }
public string Images { get; set; }
public int CompletedFlag { get; set; }
//odd items for manipulating data
public string curDate { get; set; }
public string boxGUIDString { get; set; }
int the db code section:
public BoxInfo[] getBox(int count)
{
string query = "select * from Box where CompletedFlag = '0'";
BoxInfo[] boxInfo = new BoxInfo[count];
//create boxinfo array
SqlCommand cmd = new SqlCommand(query, oConn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
for (int i = 0; i < count; i++)
{
boxInfo[i] = new BoxInfo();
// string test = rdr[0].ToString();
boxInfo[i].BoxGUID = new Guid(rdr[0].ToString());
boxInfo[i].BoxID = (int)rdr[1];
boxInfo[i].Date = (DateTime)rdr[2];
boxInfo[i].BCAppearance = rdr[3].ToString();
boxInfo[i].BCTrash = rdr[4].ToString();
boxInfo[i].BCPad = rdr[5].ToString();
boxInfo[i].BCClean = rdr[6].ToString();
boxInfo[i].BCStrap = rdr[7].ToString();
boxInfo[i].BCDoc = rdr[8].ToString();
boxInfo[i].BCMaint = rdr[9].ToString();
boxInfo[i].BCMaintNote = rdr[10].ToString();
boxInfo[i].UBWrap = rdr[11].ToString();
boxInfo[i].UBPiece = rdr[12].ToString();
boxInfo[i].NPSec = rdr[13].ToString();
boxInfo[i].NPDmg = rdr[14].ToString();
boxInfo[i].NPHardware = rdr[15].ToString();
boxInfo[i].TruckNum = rdr[16].ToString();
boxInfo[i].Images = rdr[17].ToString();
boxInfo[i].CompletedFlag = 0;
}
}
return boxInfo;
}
in the controller
namespace BoxCheckInApp.Controllers
{
public class AddBoxController : Controller
{
//
// GET: /AddBox/
public CodeDB D = new CodeDB();
public CodeODB O = new CodeODB();
public BoxInfo box = new BoxInfo();
public ActionResult Boxes()
{
D.Open();
int count = D.getRows();
if (count != 0)
{
BoxInfo[] myBoxes = new BoxInfo[count];
myBoxes = D.getBox(count);
D.Close();
return View();
}
else
{
String noBoxes = "There are no incomlete boxes to edit";
D.Close();
return View();
}
}
in the controller I need to pass myBoxes to the view. My boxes is a BoxInfo object Array. Each object is one row of the database.