asp.net的MVC和SQL查询 [英] asp.net mvc and sql queries

查看:143
本文介绍了asp.net的MVC和SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发网站使用网页形式,现在我已经在这里我使用MVC3框架与Rzor的项目。我的问题是关于在MVC中的一些基本的设计模式。我有一个网页,其中在左边,我会从SQL表拉分类,在我中心将查询另一个SQL表,和几个遍布页面。

I develop websites using web-forms, now I have a project where I am using MVC3 Framework with Rzor. My question is about some basic design patterns in MVC. I have a Webpage, where on left side i will Pull Categories from SQL Table, In Center I will Query another Sql Table, and few more all over the page.

所以我的问题是什么?将数据导入一个网页的最佳方式,所有这些查询是完全独立的,我需要为每个查询创造新的模式?或者有这样做的更好的办法?

So my question is...whats the best way to bring data into one webpage, all these queries are totally independant, do I need to create new MODEL for every Query? or there is a better way of doing it?

在我的WebForms使用的用户控件,其中每个用户的控制有其自身的设计和放大器; SQL查询。我听说过的MVC使用部分的意见,但我不知道,我想我有很难理解如何使用不同的查询和放大器将数据到一个网页;显示在网页上输出。

in WebForms I used user controls, where every user control had its own design & Sql Queries. I have heard about using Partial Views in MVC, but i am not sure, I guess i am having hard time understanding how to bring data into one webpage using different queries & show output on webpage.

感谢

推荐答案

您应该创建一个视图模型。的看看下面的更新

这是重新presents你的页面的模型。你想在视图中显示的内容应该在你的ViewModel存在。您将填充视图模型在你的控制器,并在页面上显示出来。

This is a model that represents your page. The elements you want to show in your view should exist in your ViewModel. You will populate the ViewModel in your controller and display them on the page.

我写了一个购物网站页面的一个例子,有在中央的左侧和产品分类。这两个实体将存在于不同的表。

I've written an example of a shopping site page, with categories on the left and Products in the centre. Both entities would exist in different tables.

例如:

class MainPageViewModel
{
  //this data is from a different table.
  //and goes on the left of the page
 public string Categories {get; set;}
  //this data is also from a different table.
  //and goes on the center of the page
 public List<Products> Products {get; set;}
}

在你的控制器:

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        MainPageViewModel vm = new MainPageViewModel();
        vm.Categories = GetCategories();
        //use the GetProducts() to get your products and add them.
        vm.Products.Add(...); 
        return View(vm); //pass it into the page
    }
    string[] GetCategories()
    {
     DataTable data = GetDataFromQuery("SELECT * FROM Categories WHERE..");
     //convert the data into a string[] and return it..
    }
    //maybe it has to return something else instead of string[]? 
    string[] GetProducts()
    {
     DataTable data = GetDataFromQuery("SELECT * FROM Products WHERE..");
     //convert the data into a string[] and return it..
    }
    DataTable GetDataFromQuery(string query)
    {
        SqlDataAdapter adap = 
             new SqlDataAdapter(query, "<your connection string>");
        DataTable data = new DataTable();
        adap.Fill(data);
        return data;
    }  
}

然后在你的看法,你适当地显示它:

Then in your view you display it appropriately:

@model MainPageViewModel 

@{ ViewBag.Title = "MainPage"; }

<div id="left-bar">
  <ul>
    @foreach (var category in Model.Categories)
    {
        <li>@category</li>
    }
  </ul>
</div>
<div id="center-content">
    <ul>
    @foreach (var product in Model.Products)
    {
        <li>@product.Name</li>
        <li>@product.Price..</li>
        .....
    }
  </ul>  
</div>


更新

这是在哪里你提到你的数据库表和列定期更改您的评论。

This is about your comment where you mentioned that your database tables and columns change regularly.

我不能肯定地说但每天也许你不应该做的表像,也许有更好的数据库设计,你可以有,或者一个RDBMS是不正确的事情为你和你应该看看到NoSQL数据库(如 MongoDB的

I can't say for sure but maybe you shouldn't be making tables like that everyday, maybe there is a better database design you could have, or maybe an RDBMS isn't the right thing for you and you should look into a NoSql database (like MongoDB )

不过,如果你继续上述code我建议把到数据层类自己的这一点。

Nevertheless if you continue with the above code I suggest putting this into a data layer class of its own.

另外看看小巧玲珑这是一个非常薄的数据访问层,仅仅从SQL查询或存储过程的数据库中获取的对象。 (只是正是你需要的),它是由和计算器使用。

Also take a look at Dapper it's a very thin data access layer that just gets objects from the database with sql queries or stored procedures. (Just exactly what you need) It's made and used by stackoverflow.

这篇关于asp.net的MVC和SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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