hesh
hesh

Reputation: 65

multiple loops in a view mvc

If I have the following code would it be possible to have two view data on a single view? How would I go about doing this Many thanks in advance Hesh

public ActionResult Index(long id = 0)
        {
            var contentPage = (from c in db.Tble_content
                               where c.id == id
                               select c);
            var contentlist = (from c in db.Tble_content
                                       where c.EN_TopPageID == id
                                       select c);
            return View();
  }

Upvotes: 0

Views: 95

Answers (1)

von v.
von v.

Reputation: 17108

A little more code would help. But assuming your Tble_content has a structure like this:

public class Tble_content {
    public int Id {get;set;}
    public string Content{get;set;}
}

you can have a viewmodel like this:

public class ContentViewModel {
    public string ContentPage {get;set;}
    public string ContentList {get;set;}
}

and you pass it to a view like this:

public ActionResult Index(long id = 0)
{
    var contentPage = (from c in db.Tble_content
                        where c.id == id
                        select c);
    var contentlist = (from c in db.Tble_content
                                where c.EN_TopPageID == id
                                select c);

    return View(new ContentViewModel {
       ContentPage = contentPage,
       ContentList = contentlist
    });
}

Upvotes: 1

Related Questions