1

This is the controller:

public ActionResult Index()
{

   var result = from u in myContext.Users
             join g in myContext.Groups
             on u.gid equals g.id
             select new {
                 u.username,
                 g.name
             };

   return View(result );
}

This is the View

@model IEnumerable<eTravel1.Models.User>
...
@Html.DisplayNameFor(model => model.username)
@Html.DisplayNameFor(model => model.name)
..

The returned anonymous data is not compatible in the View. I would really apreciate it if someone could give some advice. Thanks

Andrei Spatar
  • 253
  • 1
  • 3
  • 10

1 Answers1

3

You should create a ViewModel Class like this:

public class UserViewModel{

    //class constructor
    public UserViewModel(String userName_p, String name_p){
         this.UserName = userName_p;
         this.Name = name_p;
    }

    //class attributes
    String UserName {get; set;}
    String Name {get; set;}
}

Create an object of that class:

var result = from u in myContext.Users
                 join g in myContext.Groups
                 on u.gid equals g.id
                 select new UserViewModel(
                     u.username,
                     g.name
                 );

And finally, pass this object to your view the same way:

//returning an IEnumerable<UserViewModel> data type
return View(result.AsEnumerable());

In your View, you should expect this exact same data type:

//change the UserViewModel to your complete namespace!
@model IEnumerable<UserViewModel>

I strongly recommend you to read this stackoverflow question about the "ViewModel Best Practices".

Hope it helps!

Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
  • Thanks for the fast response! When using select new UserViewModel{ u.username, g.name }; Cannot initialize type 'UserViewModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable' message is displayed. Also on view, how can I use two models? (since I use Users and Groups tables) Many thanks. – Andrei Spatar May 01 '12 at 22:14
  • How are you defining the variable `var result`? Did you add the constructor to the class UserViewModel? – Cacho Santa May 01 '12 at 22:29
  • I just edited my answer, it should be `new UserViewModel( u.username, g.name );`... change { } for ( ) – Cacho Santa May 01 '12 at 22:35
  • Yes, did the modifications, created the constructor. When the Index page is displayed, I get the following:The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[eTravel1.Controllers.UserViewModel]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[eTravel1.Models.User]'. – Andrei Spatar May 01 '12 at 22:40
  • that is because your data types are different(the one you return in the controller and the one you receive in the view), I've just edited my answer again.... – Cacho Santa May 01 '12 at 22:44
  • @AndreiSpatar did you solve it? If you did, can you mark the response as answer to your question please? – Cacho Santa May 01 '12 at 23:40
  • 1
    Thank for your help! I can sleep now. – Andrei Spatar May 01 '12 at 23:42