Home > Back-end >  Creating a list to produce on View without using a foreach statement in the View
Creating a list to produce on View without using a foreach statement in the View

Time:08-27

I am trying to produce a list of Viewbag items in the controller with a foreach statement. Then use those ViewBag items in the View. Like this shows.

enter image description here

My issue is that I do not want to use a Foreach statement in the view. The View has images pulled from the database and are on the view in a pattern (Simular to Above). So the Foreach statement in the View will not give me what I need. The images are held in a Row/Column configuration and not all areas will have an Image. I know how to do a Foreach statement, my issue is iterating over the records and giving the ViewBag a number or name and number. Like ViewBag.Name[i] = the item. So the Comments below are fine for developing a repeating list of items in a table etc. I know how to do that.

I have two pieces of code from the controller and neither of them are producing a list of ViewBag items.

  var list = db.TCards.OrderBy(x => Guid.NewGuid()).Take(10).ToList();
  foreach(var item in list)
  {
     ViewBag.name = item.CardName;
  }
  return View();

The best result would be something like this ViewBag.name[i] = item.CardName; This second one is really not working although would give me the best scenario for iteration. I just do not know how to pull the image name I need for the View. item.Cardname

  var list = db.TCards.OrderBy(x => Guid.NewGuid()).Take(10).ToList();
  for (var i = 0; i < 10; i  )
  {
     ViewBag.Name[i] = list[i];
  }      
  return View();

This may be a stretch and maybe cannot be done. thanks for your help!

UPDATE: Changed and Added some things above. What I am attempting to do is develop a list in the Controller. So that I do not have to do a Foreach or For in the View. Creating a set of ViewBag.Name[i].CardURL and placing that in the view as @ViewBag.Name1.CardUrl or something like that. The List has to be complete in the Controller first.

@ViewBag.Name1.CardUrl, @ViewBag.Name2.CardUrl, @ViewBag.Name3.CardUrl, @ViewBag.Name4.CardUrl etc.

So when I put them in the view it would look like this:

       <div >
            <div ></div>
            <div >
                <img src="~/Content/Images/@ViewBag.Name1.CardURL" style="width:145px; padding-bottom:15px; padding-top:100px;" />
            </div>
            <div ></div>
            <div >
                <img src="~/Content/Images/@ViewBag.Name2.CardURL" style="width:145px; padding-top:100px;" />
            </div>
            <div ></div>

        </div>

And then repeat in another row with a set of columns. Maybe there is another way to do this?

CodePudding user response:

You can simply assign list into ViewBag.Name

  var list = db.TCards.OrderBy(x => Guid.NewGuid()).Take(10).ToList();
  ViewBag.Name = list;
  return View();

And in your view, you can get the data with foreach or using index with for

<!-- get the data with foreach -->
@foreach (var item in ViewBag.Name)
{
    @item.CardName
}

<!-- or get the data using index with for -->
@for (var i = 0; i < ViewBag.Name.Count; i  )
{
    @ViewBag.Name[i].CardName
}

CodePudding user response:

This is the solution I found to work and I am happy with the results, just not happy of how I had to do it. So if someone has a better way to achieve this, Please provide an answer.

Controller:

public ActionResult Reading()
{
   var list = db.TCards.OrderBy(x => Guid.NewGuid()).Take(2);
   ViewBag.Name = list;

   return View(list);
}

I changed it to Take(2) so that it would be more random. I was getting dupes sometimes.

CHTML View: Very Ugly!

    @foreach (var item in Model)
    {
        ViewBag.name = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name2 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name3 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name4 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name5 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name6 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name7 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name8 = item.CardURL;
    }
    @foreach (var item in Model)
    {
        ViewBag.name9 = item.CardURL;
    }
 <div >
        <div ></div>
        <div >
            <img src="~/Content/Images/@ViewBag.Name1.CardURL" style="width:145px; padding-bottom:15px; padding-top:100px;" />
        </div>
        <div ></div>
        <div >
            <img src="~/Content/Images/@ViewBag.Name2.CardURL" style="width:145px; padding-top:100px;" />
        </div>
        <div ></div>

    </div>

And so on through nine. I can put the viewbag.name1 - 9 anywhere I want on the page.

  • Related