ASP.NET MVC C#:将在多个表/查询数据到视图 [英] ASP.NET MVC C#: Bringing in data from multiple tables/queries into a view

查看:1186
本文介绍了ASP.NET MVC C#:将在多个表/查询数据到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我仍然得到asp.net的窍门和MVC框架,并从传统的ASP和VB转换过我的知识 - 所以请温柔

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.

我有我的第一个视图(/家/信息/ X)运作良好,<一个href=\"http://stackoverflow.com/questions/1210413/asp-net-mvc-c-object-reference-errors-when-going-to-view-record-details\">thanks以previous帮助指着我在正确的方向,现在我需要从多个表和查询数据添加/欣赏到MVC视图(我恨SQL和MVC都使用了不同的含义字视图)

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

我不是在找人写答案,我(除非他们真的感觉精力充沛),更因此有人指出我的什么,我应该看和读了,了解正确的方向它,这样做我自己。

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.

我的问题

有我需要在此视图中显示多个数据集,每个不同的数据集具有/ FK 1-M的关系建立了适当的PK,将所得记录将需要通过要环

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.

如何,我会pviously这样做$ P $

在我传统的ASP天,我就刚才定义在页面头部的SQL查询,其中数据是使用,与沿线的select语句:

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:

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

一旦做,你会设置做而不是QUERY_NAME BOF / EOF起来,然后你从想要查询的字段名下降,这是所有做的。

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.

我现在该如何达致这?

所以,从我传统的ASP知识快速转发,我怎么acheive相同的结果与MVC?

So, fast forwarding from my classic ASP knowledge, how do I acheive the same outcome with MVC?

该表/视图我希望用我的数据模型中已经定义(和关系正出现在那里,我会假设是一个加号),我只需要找出我怎么可以在网页中调用这些并使用记录的ID显示在详细信息视图,以保证只显示相关的数据。

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.

在此先感谢

推荐答案

您正在寻找的被称为<一个概念href=\"http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx\">ViewModel.本质上,这是你写的,其中包含将在您的视图中使用的所有数据的自定义类。因此,它是负责将来自不同的表合并的所有数据,并使其暴露作为属性。如果您使用的是数据访问层,这是经常带来一些实体一起简单。如果您使用原始SQL这样做,那么你会执行查询时性能进行了访问。

The concept you are looking for is called a 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.

然后,你将使你的观继承视图模型,就像这样:

Then you would make your View inherit from the ViewModel, like so:

<%@ 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) %>

要从您的控制器构造你的看法,创建类(MyViewModel)的一个新实例,它传递的详细信息记录,你需要的ID,并在你的类逻辑将得到正确的数据服务。然后,从您的控制器像正常返回你的看法。

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. Then return your view from your controller like normal.

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

这篇关于ASP.NET MVC C#:将在多个表/查询数据到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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