简体   繁体   English

asp.net MVC3 C#:将查询作为参数传递并显示结果

[英]asp.net MVC3 C#: Passing query as a parameter and displaying result

I am new to asp.net , C# and building an MVC application based on the popular Music Store application. 我是asp.net,C#的新手,并且基于流行的音乐商店应用程序构建MVC应用程序。 I have my basic navigation ready and I have reached a point where I am drawing a complete blank. 我准备好了基本的导航功能,并且我已经达到了完全空白的地步。 Basically, my asp page displays a SQL query (which is saved in SQL DB on same machine) 基本上,我的asp页面显示一个SQL查询(在同一台机器上保存在SQL DB中)

Need: I need to have a button next to this query which when clicked, connects to another DB through OLEDB, and runs the query and shows result in a pop up window. 需要:我需要在此查询旁边有一个按钮,当单击该按钮时,通过OLEDB连接到另一个DB,并运行查询并在弹出窗口中显示结果。

Questions: How do I pass the query (which is being fetched from DB) as a parameter to code below and How do I make the results pop up in a window. 问题:如何将查询(从DB中获取)作为参数传递给下面的代码,如何在窗口中弹出结果。

Can you please point me in correct direction. 你能指点我正确的方向吗? The code below is from a stand alson asp page which i used for testing connections etc. basically i need to pass the query as a parameter (replacing query seen below) and have the result in a pop window. 下面的代码来自一个用于测试连接等的alson asp页面。基本上我需要将查询作为参数传递(替换下面的查询)并将结果放在弹出窗口中。

<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data.Odbc" %>

<script runat="server">
sub Page_Load
        Dim dbconn, sql, dbcomm, dbread
        dbconn = New OleDbConnection("Provider=xxxx;Password=xxxx;User ID=xxxx;Data Source=xxxx;Initial Catalog=xxxx;Port=xxxx;")
dbconn.Open()
        sql = "Select ID from TABLE1"
        dbcomm = New OleDbCommand(sql, dbconn)
        dbread = dbcomm.ExecuteReader() <%-- Call this method within oledbcommand--%>

  If dbread.Read = False Then
                    MsgBox("No Data Check")
                Else                    
                  Response.Write("<table>")
                    Do While dbread.Read()
                        Response.Write("<tr>")
                        Response.Write("<td>")
                        Response.Write(dbread(0))
                        Response.Write("</td>")
                        Response.Write("</tr>")
                    Loop
                  Response.Write("</table>")

 End If
         dbconn.Close()

end sub
</script>

ADDITIONAL DETAILS 额外细节

CONTROLLER CLASS . 控制器类。 .

public ActionResult DisplayResult(String Qry)        
   {
       List<QuerySet> QueryToExecute = new List<QuerySet>();
       return View(QueryToExecute);    

VIEW that provides this contoller with DATA, this is query that is fetched from my SQL DB and should be executed to a separate DB on a separate server. 为此控制器提供DATA的VIEW,这是从我的SQL DB获取的查询,应该在单独的服务器上执行单独的DB。

<ul> 
@foreach (var ShowQueries in Model.Queriess)
{ 
    <li> 
       @Html.ActionLink(ShowQueries.Query, "DisplayResult", new { Qry = ShowQueries.Query }) 
    </li> 
} 

ISSUE: How should I use a view named 'DisplayResult' which handles the query fetched by view above and executes it agaisnt another DB. 问题:我应该如何使用名为'DisplayResult'的视图来处理上面视图提取的查询并执行另一个DB。 I was hoping I can use a Webform view rather than a razor view but either way i am not able to pass the parameter Any ideas are appreciated 我希望我可以使用Webform视图而不是剃刀视图,但无论哪种方式我都无法传递参数任何想法都赞赏

The point of MVC is to move data connections out of the View (aspx page) and into a Controller. MVC的目的是将数据连接移出View(aspx页面)并进入Controller。

Read some more MVC tutorials, and buy a book or two. 阅读更多MVC教程,并购买一两本书。 You should actually populate the data into a viewmodel on the controller, and then pass that viewmodel to the view. 实际上,您应该将数据填充到控制器上的viewmodel中,然后将该viewmodel传递给视图。 This way, the view knows nothing about how to get the data -- it already got it from the controller. 这样,视图对如何获取数据一无所知 - 它已经从控制器获取了数据。

Views should have the responsibility of displaying data to users over the web, not getting the data or manipulating it directly. 视图应负责通过Web向用户显示数据,而不是直接获取数据或操作数据。

With that aside, here is how you would do it: 除此之外,以下是您将如何做到这一点:

Pass the query as a string to an Action Method on a Controller (using HTTP POST or GET) using AJAX (ie jQuery $.ajax() method). 使用AJAX(即jQuery $ .ajax()方法)将查询作为字符串传递给Controller上的Action方法(使用HTTP POST或GET)。

Have the action method return the HTML for your popup window, using a Partial View. 让action方法使用Partial View返回弹出窗口的HTML。 You could also return Json, but I think HTML / partial view would be easier in this case. 您也可以返回Json,但我认为在这种情况下HTML /部分视图会更容易。 This is the method that will do your OLE DB connection, and execute the query. 这是将执行OLE DB连接并执行查询的方法。

In your $.ajax() success function callback, write javascript that will popup a new dialog with the partial view HTML that was returned by the controller action method. 在$ .ajax()成功函数回调中,编写javascript,它将弹出一个新的对话框,其中包含控制器操作方法返回的部分视图HTML。

You could create a class to hold the data you want to display: 您可以创建一个类来保存要显示的数据:

namespace sample {
    class viewList
    {
        public string field1 {get;set;}
        ...
    }
}

and create a list to hold your results in your controller: 并创建一个列表以将结果保存在控制器中:

List<viewList> theList = new List<viewList>();

//Populate dbread here...

while (dbread.Read())
{    
    viewList listData = new viewList();
    listData.field1 = (dataType)dbread[0]; //Convert to your data type
    theList.Add(listData);
}

and pass this to the view: 并将其传递给视图:

return view(theList);

Then in your model (of model type viewList ) display your results in a table: 然后在您的模型(模型类型viewListviewList结果显示在表中:

@model sample.viewList

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.field1</td>
        </tr>
    }
</table>

ALTERNATIVE 备选

To display in popup, put the list into the ViewBag like this: 要在弹出窗口中显示,请将列表放入ViewBag,如​​下所示:

List<viewList> theList = new List<viewList>();

//Populate dbread here...

while (dbread.Read())
{    
    viewList listData = new viewList();
    listData.field1 = (dataType)dbread[0];
    theList.Add(listData);
}

ViewBag.Items = theList;

Then in your view: 然后在你看来:

<script type="text/javascript">
    $(function() {
        var array = @Html.Raw(Json.Encode(ViewBag.Items));
        //Construct your table using the array here...
            alert(theConstructedTable);
    });
</script>

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

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