我可以将匿名类型传递给我的 ASP.NET MVC 视图吗? [英] Can I pass an anonymous type to my ASP.NET MVC view?

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

问题描述

我刚刚开始使用 ASP.NET MVC,因为它处于测试阶段.在我的代码中,我正在运行一个简单的 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.

是否可以通过接受 IEnumerable 作为其视图数据的方式声明 ASP.NET MVC 视图,其中 T 由从控制器传递的内容定义,或者我是否必须定义一个具体类型来填充来自我的查询?

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.

想一想.当您为视图声明模型类型时,您就可以获得视图的智能感知.这意味着必须在编译时确定类型.但问题是,我们能否从运行时提供给它的东西中确定类型.当然可以,但不能保留强类型.

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.

对于一种您还不知道的类型,您将如何获得 Intellisense?控制器最终可能会在运行时将任何类型传递给视图.我们甚至无法分析代码和猜测,因为动作过滤器可能会更改传递给视图的对象,因为我们知道.

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天全站免登陆