如何从mvc3中的两个表中获取数据 [英] how to fetch data from two tables in mvc3

查看:91
本文介绍了如何从mvc3中的两个表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC3使用Razor



我有两个非关系表(没有外键)。

我想要从单个视图中的两个表中获取数据。

怎么做?

请提供任何示例代码..

ASP.NET MVC3 Using Razor

I have two tables which are not relational(without foreign key).
and i want to fetch the data from both the table on a single view.
how to do that ?
please provide any sample code..

推荐答案

创建一个ViewModel例如



Create a ViewModel for example

public class YourViewModel
{
public YourViewModel()
{

FirstTable=new List<type>();
SecondTable=new List<type>();
}
public List<type> FirstTable{get;set;}
public List<type> SecondTable{get;set;}
}



在将Viewmodel返回到以下视图的同时加载数据




Load the data while returning Viewmodel in to the View like below

[HttpGet]
public ActionResult YourAction()
{
var viewModel=new YourViewModel();
viewModel.FirstTable=YourFirstCollection();//your list of collection
viewModel.SecondTable=YourSecondCollection();your list of collection
return View(viewModel);
}





从视图中你可以像 Model.FirstTable和Model.SecondTable

希望这有助于



From the view you can iterate your collection like Model.FirstTable and Model.SecondTable
Hope this helps


这篇关于如何从mvc3中的两个表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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