简体   繁体   English

ASP.NET MVC C#:将来自多个表/查询的数据引入视图

[英]ASP.NET MVC C#: Bringing in data from multiple tables/queries into a view

Ok, I'm still getting the hang of asp.net and the MVC framework and converting my knowledge over from classic ASP and VB - so please be gentle. 好的,我仍然掌握asp.net和MVC框架,并将我的知识从经典的ASP和VB转换过来-所以请保持温柔。

I've got my first view (/home/details/X) functioning well thanks to previous help pointing me in the right direction , now I need to add data from multiple tables and queries/views to the MVC view (I hate that SQL and MVC both use the word view for different meanings). 由于先前的帮助将我指向正确的方向 ,因此我的第一个视图(/ home / details / X)运行良好,现在我需要将多个表和查询/视图中的数据添加到MVC视图中(我讨厌SQL和MVC都使用单词view表示不同的含义)。

I'm not looking for someone to write the answer for me (unless they're feeling really energetic), more so for someone to point me in the right direction of what I should be looking at and reading up on to understand it and do this myself. 我不是在寻找别人为我写答案的人(除非他们觉得自己真的很精力充沛),更何况是有人将我指向我应该看的正确方向,并继续阅读以理解并执行这个我自己。

My problem 我的问题

There are multiple datasets which I need to display in this view, and each different data set has a proper PK/FK 1-M relationship established, and the resultant records would need to be looped through. 我需要在此视图中显示多个数据集,并且每个不同的数据集都建立了正确的PK / FK 1-M关系,并且需要循环浏览所得的记录。

How I would have done this previously 我以前是怎么做到的

In my classic ASP days, I would have just defined the SQL query at the head of the page where the data was to be used, with a select statement along the lines of: 在我经典的ASP时代,我会在要使用数据的页面顶部定义SQL查询,并使用select语句,如下所示:

SELECT * FROM query_name
WHERE query_uniquecolumnname = Request.QueryString("value")

Once that was done, you'd set the do while query_name NOT BOF/EOF up, then drop in the field names you wanted from that query and it was all done. 完成此操作后,您可以将query_name NOT BOF / EOF设置为while,然后放入您要从该查询中获取的字段名称,此操作就完成了。

How do I acheive this now? 我现在该如何实现?

So, fast forwarding from my classic ASP knowledge, how do I acheive the same outcome with MVC? 因此,根据我的经典ASP知识快速转发,我如何使用MVC达到相同的结果?

The tables/views I wish to use are already defined within my data model (and the relationships are showing up in there which I would assume is a plus), I just need to work out how I could call these within the page and use the ID of the record being displayed in the Details view to ensure only related data is displayed. 我希望使用的表/视图已经在我的数据模型中定义了(并且关系在其中显示,我认为这是一个加号),我只需要弄清楚如何在页面中调用这些表/视图并使用在“详细信息”视图中显示的记录的ID,以确保仅显示相关数据。

Thanks in advance 提前致谢

The concept you are looking for is called a ViewModel . 您正在寻找的概念称为ViewModel Essentially this is a custom class that you write that contains all the data that would be used in your view. 本质上,这是您编写的自定义类,其中包含视图中将使用的所有数据。 So it is responsible for amalgamating all the data from the different tables and exposing it as properties. 因此,它负责合并来自不同表的所有数据并将其作为属性公开。 If you're using a data access layer, this is often as simple as bringing a few entities together. 如果您使用的是数据访问层,那么这通常就像将几个实体放在一起一样简单。 If you're using raw SQL to do it, then you would execute your queries when the properties were accessed. 如果您使用原始SQL来执行此操作,则将在访问属性时执行查询。

Then you would make your View inherit from the ViewModel, like so: 然后,使View从ViewModel继承,如下所示:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
 Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.MyViewModel>" %>

Now in your View, you can access all the different properties of your object simply by writing statements like: 现在,在视图中,您只需编写如下语句即可访问对象的所有不同属性:

<%= Html.TextBox("MyProperty", Model.MyProperty) %>

To construct your view from your controller, create a new instance of your class (MyViewModel), pass it the ID of the details record that you need, and the logic in your class will take care of getting the right data. 要从控制器构造视图,请创建类的新实例(MyViewModel),并向其传递所需的详细信息记录的ID,并且类中的逻辑将负责获取正确的数据。 Then return your view from your controller like normal. 然后像平常一样从控制器返回您的视图。

   var myDetailsModel = new MyViewModel(detailsID);
   return View(myDetailsModel);

I would recommend reading this primer on ASP.NET MVC 我建议阅读有关ASP.NET MVC的入门知识

http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx

It covers most basic scenarios you'll need to get up and running. 它涵盖了启动和运行所需的大多数基本方案。

If however you want to combine multiple resultsets into one, and then return it as a view, you should create a custom object, and map the resultset to it, then you can bind against your custom object in the view. 但是,如果要将多个结果集合并为一个,然后将其作为视图返回,则应创建一个自定义对象,并将结果集映射到它,然后可以在视图中绑定到您的自定义对象。

When I need to display multiple things like this on a web page, I use typically use RenderAction to do it. 当我需要在网页上显示多个类似内容时,通常使用RenderAction来完成。

RenderAction allows you to use a controller method dedicated to that particular part of the view (a subview, in effect), so you can pass a single data set of strongly-typed data to that "subview". RenderAction允许您使用专用于视图的特定部分(实际上是子视图)的控制器方法,因此您可以将单个强类型数据集传递给该“子视图”。

RenderAction is in the Microsoft.Web.Mvc ("futures") assembly. RenderAction在Microsoft.Web.Mvc(“期货”)程序集中。

If you are new at all of this, I apologize; 如果您是这方面的新手,我深表歉意。 this is a bit bleeding edge, but you're going to need to know it anyway. 这有点前沿,但是无论如何您都需要知道它。 Be sure to check out the NerdDinner tutorial first. 请务必先查看NerdDinner教程。

http://www.andreas-schlapsi.com/2008/11/01/renderaction-and-subcontrollers-in-aspnet-mvc/ http://www.andreas-schlapsi.com/2008/11/01/renderaction-and-subcontrollers-in-aspnet-mvc/

http://davidhayden.com/blog/dave/archive/2009/04/04/... http://davidhayden.com/blog/dave/archive/2009/04/04 / ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM