English 中文(简体)
MVC - 通过多个数据表以观察
原标题:MVC - Passing multiple data tables to a view

我目前在我的MVC项目主计长办公室有以下代码:

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

是否有办法让这种观点接受两种模式,或者,或者说是合并两种结果组合的办法(两个表格没有任何关联?)

最佳回答

是的,但并非完全如此。 你们想做的是,创造一种习俗观念。 这个类别(MyPageViewModel)将具有两个(或两个以上)特性,每个物体各有一个。 您认为,您将使用<代码>查阅。 模型.Table1Data 和Model.Table2Data

缩略语

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

请见view is need to be pledges/strong> to thistom ViewModel category.

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

Don t试图打上你自己的字;更容易形成新的观点,检查“明显分类”的观点,并具体说明你的新的习俗观点。

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);
  }
}
问题回答

是——设立一个新类别——你将使用这一模式——包含两个表格:

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

之后,在你的控制员中,这一类财产先入住,同时居住,并寄给您。 然后修改观点,承认这种新类型为观点模式。

为什么不要在你们的模式中增加一个阶层?

public class MyModel {

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

}

然后,你将我的摩德尔带给观点负责人。

关于控制器:

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

我通过编制一个表格清单解决这一问题,并将这份表格传递给我的看法模式。 这基本上是一份交易意向清单。 我的解决方案被称作DAL,在模式中,我建立了一种交易实体,代表交易。

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

我把交易实体名单(电话1、电话2、电话3)与我的电网连接起来,然后在我的电话会议上添加三个电话物体(如一张纸张),并将之交给我的GisData。 模式。

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

在业余档案中,我先拿到清单,然后通过每个表格(ElementAt(#))进行检索,这三栏不同,每栏各栏各一个。 BTW,你可以忽略反变量。

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

你们可以把这两者都纳入单一的观点:

Model Defin:

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(主计长):

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

您可能必须使用。 ViewModel。 你界定了等级,这将包括你想要的两种类别(加上任何其他额外性质),然后,你将这作为模式。

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

然后,你才准备收集这些观点,随后可以查阅:

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

等等。 仅将你们所需要的所有实体合并成一个类别,并在主计长和观点中将这一类别作为你的模式。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签