MVC - 将多个数据表传递给视图

发布于 2024-10-28 01:01:54 字数 867 浏览 2 评论 0原文

目前,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟凡古楼 2024-11-04 01:01:54

是的,有,但不完全是那样。完成您想做的事情的方法是创建一个自定义 ViewModel 类。此类 (MyPageViewModel) 将具有两个(或更多)属性,每个属性对应一个对象。在您看来,您将使用 Model.Table1DataModel.Table2Data 访问它们。

自定义 ViewModel 类非常简单:

public class MyPageViewModel
{
   public IQueryable<Table1Data> Table1Data { get; set; }
   public IQueryable<Table2Data> Table2Data { get; set; }
}

视图需要强类型化到此自定义 ViewModel 类。

<%@ Page Title="MyPage" MasterPageFile="~/Application/Master Pages/Site.Master"
    Inherits="System.Web.Mvc.ViewPage(Of MyAppNamespace.MyPageViewModel)" %>

不要尝试自己输入;更容易创建新视图并检查“强类型”视图,并指定新的自定义视图模型类。

那么你的操作控制器方法将是:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    MyDataContext dc = new MyDataContext();

    MyPageViewModel vm = new MyPageViewModel();

    vm.Table1Data =  from n in dc.Table1                     
                     select n;

    vm.Table1Data = from k in dc.Table2        
                    select k;

    return View(vm);
  }
}

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 and Model.Table2Data.

A custom ViewModel class is very simple:

public class MyPageViewModel
{
   public IQueryable<Table1Data> Table1Data { get; set; }
   public IQueryable<Table2Data> Table2Data { get; set; }
}

You view would need to be strongly typed to this custom ViewModel class.

<%@ Page Title="MyPage" MasterPageFile="~/Application/Master Pages/Site.Master"
    Inherits="System.Web.Mvc.ViewPage(Of MyAppNamespace.MyPageViewModel)" %>

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:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    MyDataContext dc = new MyDataContext();

    MyPageViewModel vm = new MyPageViewModel();

    vm.Table1Data =  from n in dc.Table1                     
                     select n;

    vm.Table1Data = from k in dc.Table2        
                    select k;

    return View(vm);
  }
}
我的影子我的梦 2024-11-04 01:01:54

是的 - 创建一个新类 - 您将用作模型 - 包含两个表:

public class MyModel {
   public IQueryable<Table1Data> Table1Data { get; set; }
   public IQueryable<Table2Data> Table2Data { get; set; }
}

然后,在您的控制器中,初始化此类并填充两个属性并将其发送到您的视图。然后,修改视图以将这种新类型识别为视图模型。

Yes - create a new class - which you will use as your model - that contains both tables:

public class MyModel {
   public IQueryable<Table1Data> Table1Data { get; set; }
   public IQueryable<Table2Data> Table2Data { get; set; }
}

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.

爱情眠于流年 2024-11-04 01:01:54

为什么不在模型中为此添加一个类呢?

public class MyModel {

public j {get; set;}
public l {get; set;}

}

然后将 MyModel 传递给视图头部的视图。

在控制器上:

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;

    MyModel myclass = new MyModel();
    myclass.j = j;
    myclass.l = l;

    return View(myclass);
}

Why don't you add a class in your models for this?

public class MyModel {

public j {get; set;}
public l {get; set;}

}

Then you pass MyModel to the view on the View's Head.

On the 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;

    MyModel myclass = new MyModel();
    myclass.j = j;
    myclass.l = l;

    return View(myclass);
}
就此别过 2024-11-04 01:01:54

我通过创建“表”列表并将其传递给我的视图模型解决了这个问题。这本质上是一个 TransactionEntities 列表的列表。仅供参考,我的解决方案名为 DAL,在模型中我创建了一个 TransactionEntity 来表示事务。

    private TransactionEntity te;
    private IEnumerable<TransactionEntity> tel1; // A list of TransactionEntities
    private IEnumerable<TransactionEntity> tel2;
    private IEnumerable<TransactionEntity> tel3;
    private IEnumerable<IEnumerable<TransactionEntity>> telCollection;

我用我的 te“行”填充事务实体列表(tel1、tel2、tel3),然后将三个“tel”对象(本质上像一个表)添加到我的 telCollection 中,并将其分配给我的 ViewData.Model。

    telCollection = new List<IEnumerable<TransactionEntity>>();
    telCollection = telCollection.Concat(new[] { tel1 });
    telCollection = telCollection.Concat(new[] { tel2 });
    telCollection = telCollection.Concat(new[] { tel3 });
    ViewData.Model = telCollection;
    return View();

然后,在 ASPX 文件中,我获取列表并迭代每个“表”(ElementAt(#)),创建三个不同的列,每个“表”对应一个列。顺便说一句,您可以忽略计数器变量。

<td>
   <% int counter = 0; %>
   <% IEnumerable<IEnumerable<DAL.Models.TransactionEntity>> tranlist = 
          (IEnumerable<IEnumerable<DAL.Models.TransactionEntity>>)ViewData.Model; %>

   <% foreach (DAL.Models.TransactionEntity te in tranlist.ElementAt(0))
      {.... create rows/columns as needed for the data in a HTML sub-table ......} %>
 </td>
 <td>
    <% counter = 0; %>
    <% foreach (DAL.Models.TransactionEntity te in tranlist.ElementAt(1))
      {..........} %>
 </td>
 <td>
    <% counter = 0; %>
    <% foreach (DAL.Models.TransactionEntity te in tranlist.ElementAt(2))
      {..........} %>
 </td>

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.

    private TransactionEntity te;
    private IEnumerable<TransactionEntity> tel1; // A list of TransactionEntities
    private IEnumerable<TransactionEntity> tel2;
    private IEnumerable<TransactionEntity> tel3;
    private IEnumerable<IEnumerable<TransactionEntity>> telCollection;

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.

    telCollection = new List<IEnumerable<TransactionEntity>>();
    telCollection = telCollection.Concat(new[] { tel1 });
    telCollection = telCollection.Concat(new[] { tel2 });
    telCollection = telCollection.Concat(new[] { tel3 });
    ViewData.Model = telCollection;
    return View();

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.

<td>
   <% int counter = 0; %>
   <% IEnumerable<IEnumerable<DAL.Models.TransactionEntity>> tranlist = 
          (IEnumerable<IEnumerable<DAL.Models.TransactionEntity>>)ViewData.Model; %>

   <% foreach (DAL.Models.TransactionEntity te in tranlist.ElementAt(0))
      {.... create rows/columns as needed for the data in a HTML sub-table ......} %>
 </td>
 <td>
    <% counter = 0; %>
    <% foreach (DAL.Models.TransactionEntity te in tranlist.ElementAt(1))
      {..........} %>
 </td>
 <td>
    <% counter = 0; %>
    <% foreach (DAL.Models.TransactionEntity te in tranlist.ElementAt(2))
      {..........} %>
 </td>
月牙弯弯 2024-11-04 01:01:54

您可以将它们都制作成一个 ViewModel:

模型定义:

public class YourModelName
{
       public IQueryable<Table1Data> FirstTableData { get; set;}
       public IQueryable<Table2Data> SecondTableData { get; set;}

       public YourModelName(IQueryable<Table1Data> d1, IQueryable<Table2Data> d2)
       {
            this.FirstTableData = d1;
            this.SecondTableData = d2;
       }
}

用法(在控制器中):

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;

         YourModelName model = new  YourModelName(j, l);

        return View(model);
    }

You could make them both into a single ViewModel:

Model Definition:

public class YourModelName
{
       public IQueryable<Table1Data> FirstTableData { get; set;}
       public IQueryable<Table2Data> SecondTableData { get; set;}

       public YourModelName(IQueryable<Table1Data> d1, IQueryable<Table2Data> d2)
       {
            this.FirstTableData = d1;
            this.SecondTableData = d2;
       }
}

Usage (in 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;

         YourModelName model = new  YourModelName(j, l);

        return View(model);
    }
柒七 2024-11-04 01:01:54

在 MVC3 之前的版本中,我会使用 ViewModel 并为您希望视图使用的每个对象添加属性。如果您使用的是 MVC3,我会看一下 ViewBag

一个简单的视图模型:

public class SomeViewModel
{
   public object Object1 { get; set; }
   public string Message { get; set; }
}

然后在控制器中将其传递给视图:

var vm = new SomeViewModel { Object1 = coolThing, Message = neatMessage };
return View(vm);

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:

public class SomeViewModel
{
   public object Object1 { get; set; }
   public string Message { get; set; }
}

Then in your controller you'd pass this to your view:

var vm = new SomeViewModel { Object1 = coolThing, Message = neatMessage };
return View(vm);
美胚控场 2024-11-04 01:01:54

您可能必须使用ViewModel。您定义类,它将包含您想要的两个类的实例(+任何其他附加属性),然后将其用作模型。

class NewViewModel {
    Table1 T1 {get;set;}
    Table2 T2 {get;set;}
    int Anything {get;set;}
}

然后,您只需准备这些 ViewModel 类的集合,然后访问它们的实例,例如:

NewViewModel m = new NewViewModel();
var a = m.T1.PropertyA;
var b = m.T2.PropertyB;

等等。只需将您需要的所有实体合并到一个类中,然后使用该类作为控制器和视图中的模型。

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.

class NewViewModel {
    Table1 T1 {get;set;}
    Table2 T2 {get;set;}
    int Anything {get;set;}
}

Then you just prepare collection of these ViewModel classes and later access instances of them like:

NewViewModel m = new NewViewModel();
var a = m.T1.PropertyA;
var b = m.T2.PropertyB;

etc.. Just merge all entities you need into one class and use this class as your model in Controller and View.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文