Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference implicitly typed model in view

I'm learning MVC and I'm trying to create an implicitly typed variable for my model but not sure how to reference it in the view @model IEnumerable<???>

Controller...

public ActionResult Users() {
    var model = from MembershipUser u in Membership.GetAllUsers()
                select new {user = u, roles = string.Join(", ", Roles.GetRolesForUser(u.UserName))};

    View(model);
}
like image 590
bflemi3 Avatar asked Dec 13 '25 05:12

bflemi3


1 Answers

You can't reference an anonymous-typed variable in the view (or anywhere outside its local scope). It's possible to use a dynamic type:

@model IEnumerable<dynamic>

But this way you don't have Intellisense/compile-time type checking; the best approach would be to just create a class for your model.

like image 137
McGarnagle Avatar answered Dec 15 '25 17:12

McGarnagle