Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass an anonymous type to my ASP.NET MVC view?

Tags:

asp.net-mvc

I've just started working with ASP.NET MVC now that it's in beta. In my code, I'm running a simple LINQ to SQL query to get a list of results and passing that to my view. This sort of thing:

var ords = from o in db.Orders            where o.OrderDate == DateTime.Today            select o;  return View(ords); 

However, in my View, I realised that I'd need to access the customer's name for each order. I started using o.Customer.Name but I'm fairly certain that this is executing a separate query for each order (because of LINQ's lazy loading).

The logical way to cut down the number of queries would be to select the customer name at the same time. Something like:

var ords = from o in db.Orders            from c in db.Customers            where o.OrderDate == DateTime.Today                and o.CustomerID == c.CustomerID            select new { o.OrderID, /* ... */, c.CustomerName };  return View(ords); 

Except now my "ords" variable is an IEnumerable of an anonymous type.

Is it possible to declare an ASP.NET MVC View in such a way that it accepts an IEnumerable as its view data where T is defined by what gets passed from the controller, or will I have to define a concrete type to populate from my query?

like image 864
Matt Hamilton Avatar asked Oct 21 '08 22:10

Matt Hamilton


People also ask

Can you pass multiple models to a view?

In MVC we cannot pass multiple models from a controller to the single view.

Is it possible to pass a model object to a view from a controller?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

Can a method return an anonymous type C#?

As in the preceding facts you cannot return an anonymous type from a method, if you do want to return one then you need to cast it to an object.


1 Answers

Can you pass it to the view? Yes, but your view won't be strongly typed. But the helpers will work. For example:

public ActionResult Foo() {   return View(new {Something="Hey, it worked!"}); }  //Using a normal ViewPage  <%= Html.TextBox("Something") %> 

That textbox should render "Hey, it worked!" as the value.

So can you define a view where T is defined by what gets passed to it from the controller? Well yes, but not at compile time obviously.

Think about it for a moment. When you declare a model type for a view, it's so you get intellisense for the view. That means the type must be determined at compile time. But the question asks, can we determine the type from something given to it at runtime. Sure, but not with strong typing preserved.

How would you get Intellisense for a type you don't even know yet? The controller could end up passing any type to the view while at runtime. We can't even analyze the code and guess, because action filters could change the object passed to the view for all we know.

I hope that clarifies the answer without obfuscating it more. :)

like image 128
Haacked Avatar answered Sep 22 '22 21:09

Haacked