MVC - 将多个数据表传递给视图
目前,我的 MVC 项目的 HomeController 中有以下代码:
public class HomeController : Controller
{
public ActionResult Index()
{
MyDataContext dc = new MyDataContext();
IQueryable<Table1Data> j =
from n in dc.Table1
select n;
return View(j);
}
这样可以正常工作,但现在我想将第二个表传递到同一视图。所以我想我应该能够做这样的事情:
public class HomeController : Controller
{
public ActionResult Index()
{
MyDataContext dc = new MyDataContext();
IQueryable<Table1Data> j =
from n in dc.Table1
select n;
IQueryable<Table2Data> l =
from k in dc.Table2
select k;
return View(j, l);
}
有没有办法让视图接受这样的两个模型,或者,有一种方法可以合并两个结果集(这两个表没有以任何方式链接?
I currently have the following code in the HomeController of my MVC project:
public class HomeController : Controller
{
public ActionResult Index()
{
MyDataContext dc = new MyDataContext();
IQueryable<Table1Data> j =
from n in dc.Table1
select n;
return View(j);
}
So that works okay, but now I want to pass a second table through to the same view. So I was thinking I should be able to do something like this:
public class HomeController : Controller
{
public ActionResult Index()
{
MyDataContext dc = new MyDataContext();
IQueryable<Table1Data> j =
from n in dc.Table1
select n;
IQueryable<Table2Data> l =
from k in dc.Table2
select k;
return View(j, l);
}
Is there a way to have the view accept two models like this or, alternatively, a way to merge the two result sets (the two tables are not linked in any way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,有,但不完全是那样。完成您想做的事情的方法是创建一个自定义 ViewModel 类。此类 (MyPageViewModel) 将具有两个(或更多)属性,每个属性对应一个对象。在您看来,您将使用
Model.Table1Data
和Model.Table2Data
访问它们。自定义 ViewModel 类非常简单:
您视图需要强类型化到此自定义 ViewModel 类。
不要尝试自己输入;更容易创建新视图并检查“强类型”视图,并指定新的自定义视图模型类。
那么你的操作控制器方法将是:
Yes there is, but not quite like that. The way to do what you wish to do is to create a custom ViewModel class. This class (MyPageViewModel) would have two (or more) properties, one for each of your objects. In your view, you would access them using
Model.Table1Data
andModel.Table2Data
.A custom ViewModel class is very simple:
You view would need to be strongly typed to this custom ViewModel class.
Don't try to type that youself; easier to create a new view and check "strongly typed" view, and specify your New Custom Viewmodel class.
Then your action Controller method would be:
是的 - 创建一个新类 - 您将用作模型 - 包含两个表:
然后,在您的控制器中,初始化此类并填充两个属性并将其发送到您的视图。然后,修改视图以将这种新类型识别为视图模型。
Yes - create a new class - which you will use as your model - that contains both tables:
Then, in your controller, initialize this class and populate both properties and send it to your view. Then, modify the view to recognize this new type as the view model.
为什么不在模型中为此添加一个类呢?
然后将 MyModel 传递给视图头部的视图。
在控制器上:
public ActionResult Index()
{
MyDataContext dc = new MyDataContext();
Why don't you add a class in your models for this?
Then you pass MyModel to the view on the View's Head.
On the controller:
public ActionResult Index()
{
MyDataContext dc = new MyDataContext();
我通过创建“表”列表并将其传递给我的视图模型解决了这个问题。这本质上是一个 TransactionEntities 列表的列表。仅供参考,我的解决方案名为 DAL,在模型中我创建了一个 TransactionEntity 来表示事务。
我用我的 te“行”填充事务实体列表(tel1、tel2、tel3),然后将三个“tel”对象(本质上像一个表)添加到我的 telCollection 中,并将其分配给我的 ViewData.Model。
然后,在 ASPX 文件中,我获取列表并迭代每个“表”(ElementAt(#)),创建三个不同的列,每个“表”对应一个列。顺便说一句,您可以忽略计数器变量。
I solved the problem by creating a list of 'tables' and passing this to my view model. This is essentially a list of a list of TransactionEntities. FYI, my solution was named DAL, and in the models I created a TransactionEntity to represent a transaction.
I populate the transaction entity lists (tel1, tel2, tel3) with my te 'rows', then add the three 'tel' objects (like a table essentially) to my telCollection and assign this to my ViewData.Model.
In the ASPX file, I then get the list and iterate through each 'table' (ElementAt(#)), creating three different columns, one for each of the tal 'tables'. BTW, you can ignore the counter variable.
您可以将它们都制作成一个 ViewModel:
模型定义:
用法(在控制器中):
You could make them both into a single ViewModel:
Model Definition:
Usage (in Controller):
在 MVC3 之前的版本中,我会使用 ViewModel 并为您希望视图使用的每个对象添加属性。如果您使用的是 MVC3,我会看一下 ViewBag。
一个简单的视图模型:
然后在控制器中将其传递给视图:
In pre-MVC3 I would have used a ViewModel and add properties for each object you want the view to use. If you're using MVC3 I'd take a look at ViewBag.
A simple view model:
Then in your controller you'd pass this to your view:
您可能必须使用ViewModel。您定义类,它将包含您想要的两个类的实例(+任何其他附加属性),然后将其用作模型。
然后,您只需准备这些 ViewModel 类的集合,然后访问它们的实例,例如:
等等。只需将您需要的所有实体合并到一个类中,然后使用该类作为控制器和视图中的模型。
You will probably have to use ViewModel. You define class, that will contain instances of both classes you want (+ any other additional properties), and then you use this as a model.
Then you just prepare collection of these ViewModel classes and later access instances of them like:
etc.. Just merge all entities you need into one class and use this class as your model in Controller and View.