Render different Partialviews randomly

Asked

Viewed 24 times

0

I have the following scenario: I have to move on to a view data from different tables (similar data, but without any relation), until then I got good. I created a class (viewmodel) that receives the data, in the Controller feed her when I return to the view, and on view give a foreach on each property and within that foreach I review the PartialView respective. So far so good.

However, I intend to "mix" these partialView, and not show first the data of a property and after that all end, go to another.

How would you display this in a "random" way? Or rather, what is the logic for displaying something "random"? Is it some for loop?

For greater understanding, follows the ViewModel, Controller and View:

Viewmodel

public class VideosManuaisIndex
{
    public IEnumerable<Video> Videos { get; set; }
    public IEnumerable<Manual> Manuais { get; set; }
}

Controller

public ActionResult Index()
{
    return View(new VideosManuaisIndex 
    { 
         Videos = db.Video.ToList().OrderBy(v => v.sistema),
         Manuais = db.Manual.ToList().OrderBy(m => m.sistema)
    });
}

View

<div class="vid-lista">
@foreach (var manual in Model.Manuais)
{
    @Html.Partial("_Manual", manual);
}

@foreach (var video in Model.Videos)
{
    @Html.Partial("_Video", video)
}
</div>

1 answer

0

Well, you could create a For Loop, and check if the values are null, I do not know if it is orthodox but would be something like:

@{

    var length = Model.Manuais.Count > Model.Videos.Count ? Model.Manuais.Count : Model.Videos.Count;

    for (int i = 0; i < length; i++)
    {
        if(Model.Manuais[i] != null)
        {
            Html.Partial("_Manual", Model.Manuais[i]);
        }

        if (Model.Videos[i] != null)
        {
            Html.Partial("_Video", Model.Videos[i]);
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.