我可以通过一个匿名类型到我的ASP.NET MVC的看法? [英] Can I pass an anonymous type to my ASP.NET MVC view?

查看:145
本文介绍了我可以通过一个匿名类型到我的ASP.NET MVC的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始,现在,它在测试版的ASP.NET MVC工作。在我的code,我运行一个简单的LINQ to SQL查询来获取结果列表并通过了我的看法。这样的事情:

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);

不过,在我看来,我意识到我需要为每个订单访问客户的名称。我开始使用 o.Customer.Name ,但我相当肯定,这是执行的每个订单单独的查询(LINQ因的延迟加载)。

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);

除了现在我的ords变量是一个匿名类型的IEnumerable

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

是否有可能在声明它接受一个I​​Enumerable作为其中T是由什么会从控制器通过定义视图的数据这样一个ASP.NET MVC视图,或将我必须定义一个具体类型来填充从我的查询?

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?

推荐答案

你可以将它传递给看法?是的,但你的观点不会被强类型。但佣工将工作。例如:

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.

所以,你可以定义,其中T是由什么获取传递给它的控制器中定义的看法?嗯,是的,但不是在编译的时候明显。

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.

想想看一会儿。当你声明一个视图模型类型,它是让您获得智能感知的观点。这意味着类型必须在编译时确定。但问题问,我们可以判断在运行时提供给它的东西的类型。当然,但不强类型$ P ​​$ pserved。

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.

您该如何获得智能感知的类型,你甚至不还不知道?该控制器可能最终会传递任何类型的观点在运行一段时间。我们甚至不能分析code和猜测,因为动作过滤器可以改变传递给视图我们都知道的对象。

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. :)

这篇关于我可以通过一个匿名类型到我的ASP.NET MVC的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆