views:

140

answers:

3

Hi guys. Can you help me how to create my own ViewData.Model from mesto?

public ActionResult Index() 
{
            ViewData["Message"] = "Moji Priatelia";
            var friends = from friend in friendsDb.FriendsTables
                          orderby friend.Priezvisko
                          select friend;
            var mesto = from mesta in friendsDb.MestoTables
                        orderby mesta.Mesto
                        select mesta;

            return View(friends);
}

when i add new view ViewData.Model have data from friends but no from mesto how can i create model for mesto? Thanks a lot i am Beginner.

A: 

Your best bet is probably to wrap your two objects in another object and pass that to your view.

Such as:

public class FriendsMestoDataContext()
{
    public IEnumerable<Friends> friends{get;set;}
    public IEnumerable<Mesto> mesto{get;set}
}

You can then pass this new object to your view and access both the friends and mesto properties which will be the two classes you're returning from your Linq queries:

public ActionResult Index() 
{
            ViewData["Message"] = "Moji Priatelia";
            var friendsData = from friend in friendsDb.FriendsTables
                          orderby friend.Priezvisko
                          select friend;
            var mestoData = from mesta in friendsDb.MestoTables
                        orderby mesta.Mesto
                        select mesta;
            var dataModel = new FriendsMestoDataContext(){
                                            friends = friendsData,
                                            mesto = mestoData}


            return View(dataModel);
}

UPDATE

As Shaharyar said below, you'll want to strongly type your view with the FriendsMestoDataContex.

Jamie Dixon
+1  A: 

Just create a ViewModel and pass it to your View:

public class IndexViewModel 
{
   public IEnumerable<Friend> Friends { get; set; }
   public IEnumerable<Mesta> Mesto { get; set; }
}

For that to work, you first need to adjust your action method with something like that:

public ActionResult Index()
{
        ViewData["Message"] = "Moji Priatelia";
        var friends = from friend in friendsDb.FriendsTables
                      orderby friend.Priezvisko
                      select friend;
        var mesto = from mesta in friendsDb.MestoTables
                    orderby mesta.Mesto
                    select mesta;

        var ViewModel = new IndexViewModel {
            Friends = friends,
            Mesto = mesto
        }

        return View(ViewModel);
}

Your View now needs to be strongly typed with the IndexViewModel type.

Accessing your members would look somewhat like that:

<%: Model.Friends %>
<%: Model.Mesto %>

ViewModels are the way to go when you need to show complex data in your views that doesn't really project the domain model.

Shaharyar
A: 

Here i have problem.

public class IndexViewModel 
    {
        public IEnumerable<Friend> Friends { get; set; }
        public IEnumerable<Mesta> Mesto { get; set; }
    }

The type or namespace name 'Friend' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'Mesta' could not be found (are you missing a using directive or an assembly reference?)

But table have names FriendTable, MestoTable if i use this names it is corect. Then i do new View via IndexViewModel but i can't use this

<%: Model.Friends %>
<%: Model.Mesto %>

this stops with error The model item passed into the dictionary is of type 'MVCApp.Controllers.IndexViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVCApp.Controllers.IndexViewModel]'

Peter M.
The data types of your two properties in the IndexViewModel need to be the same as the data types being returned by your two linq queries. Our examples might not be exactly the data types being returned by your queries. Check the data type and then set the IEnumerable<myReturnedType> for each of the properties.
Jamie Dixon
Yeah, you need to update your Views Type.It has to be of type `System.Web.Mvc.ViewPage<MVCApp.Controllers.IndexViewModel>` and not`System.Web.Mvc.ViewPage<IEnumerable<MVCApp.Controllers.IndexViewModel>>`Hope this helps.
Shaharyar
Thanks a lot. Now it is going like i need :)
Peter M.
Please mark the answer as accepted if it worked out for you, thanks :-)
Shaharyar