3

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.

2 Answers 2

1

Ok so I have been playing around with it a bit and realized two things, I made an error in my loop, and I figured it out.

in the db section I corrected the loop

     do
            {
                boxInfo[i] = new BoxInfo();
                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;
                i++;
            } while (i < count - 1);

Then in the controller I used viewbag

     public ActionResult Boxes()
    {
        D.Open();
        int count = D.getRows();

        if (count != 0)
        {
            BoxInfo[] myBoxes = new BoxInfo[count];
            myBoxes = D.getBox(count);
            D.Close();

            ViewBag.boxes = myBoxes;
            return View(myBoxes);
        }
        else
        {
            String noBoxes = "There are no incomplete boxes to edit";
            D.Close();
            return View();
        }
    }

I realize I need to fix my else section which I will do next

then in the view

     @model BoxCheckInApp.Models.BoxInfo[]

     @{
        ViewBag.Title = "Boxes";
      }

      <h2>Boxes</h2>


      @foreach (BoxCheckInApp.Models.BoxInfo box in ViewBag.boxes)
      {
      <li>@box.BoxGUID</li>
      }

Apparently you need to change what you call @model to an array for it to work, and then you have to call the namespace. I did not find any help regarding this online, so hopefully it will help someone else out there.

Sign up to request clarification or add additional context in comments.

Comments

0

You are almost there with your Controller and View regarding the model. You don't need the ViewBag in this scenario.

public ActionResult Boxes() {
    D.Open();
    int count = D.getRows();

    if (count != 0) {
        BoxInfo[] myBoxes = new BoxInfo[count];
        myBoxes = D.getBox(count);
        D.Close();
        //just pass the model to the view.
        return View(myBoxes);
    } else {
        String noBoxes = "There are no incomplete boxes to edit";
        D.Close();
        return View();
    }
}

Once you pass the model to the View that is all you need to be able to access it after setting the @model. You get it by calling Model property in the View.

@model BoxCheckInApp.Models.BoxInfo[]
@{
    ViewBag.Title = "Boxes";
}

<h2>Boxes</h2>

@foreach (var @box in Model) {
  <li>@box.BoxGUID</li>
}

by specifying @model BoxCheckInApp.Models.BoxInfo[] you let the Razor engine know what is the type in Model property.

2 Comments

This solution worked as well, is there any downside to using the view bag over this method in terms of performance?
Performance, no. They all end up in the ViewData of the controller. usability however is better as the Model is a concrete type while the ViewBag is dynamic. msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.