0

又一个 MVC 问题。

我正在开发一个 Web 应用程序工具,它​​可以处理很多数据库请求等。而这一页让我在 MVC 中遇到了很多困难。

我之前在 C# 中创建了页面,这就是它的样子 数据搜索

您最多可以显示 4 个彼此相邻的网格以比较值。

现在在 MVC 中,我正在考虑以这种方式解决问题:

流程图

但是,我总是收到通常基于连接问题的随机错误消息......我尝试了很多不同的东西,这就是为什么我的代码现在有点混乱,但这里是最重要的部分。(示例只有 2 个选项)

看法

通过 MainDDL1、SubDDL1、Search1、MainDDL2、SubDDL2、Search2(这工作)

控制器

public PartialViewResult getGrid1(string MainDDL1, string SubDDL1, string Search1)
    {
        DataSearchModel voModel = new DataSearchModel();
        voModel.dtResultSet1 = DLA.DataSearchContext.getResultSet1(MainDDL1, SubDDL1, Search1);
        return PartialView(MainDDL1, voModel);
    }

public PartialViewResult getGrid2(string MainDDL2, string SubDDL2, string Search2)
{
    DataSearchModel voModel = new DataSearchModel();
    voModel.dtResultSet2 = DLA.DataSearchContext.getResultSet2(MainDDL2, SubDDL2, Search2);
    return PartialView(MainDDL2, voModel);
}

public ViewResult DataSearch(string text)
{
    DataSearchModel oModel = new DataSearchModel();
    oModel.alMainDDL = DLA.DataSearchContext.getMainDDL();

    return View(oModel);
}

我真的不喜欢我必须使用 dtResultSet1 和 dtResultSet2 而不是仅仅调用相同的方法。为什么我不能只调用 getResultSet、dtResultSet 等?!由于我创建了新模型,模型应该有自己的方法吗?或者是一个模型不能与一个对象相提并论。

模型

dtResultSet1、dtResultSet2、搜索字符串等的声明。

语境

        public static DataTable getResultSet1(string sChoice, string sFeat, string sSearch)
        {
            return setResultSet1(sChoice, sFeat, sSearch);
        }

private static DataTable setResultSet1(string sChoice, string sFeat, string sSearch)
        {

            DataTable dtTemp = new DataTable();
            string sQuery = setSqlQuery(sChoice, sFeat, sSearch);

            OleDbConnection dbConnection = null;
            // Instantiate the Connection Object
            dbConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDataBase"].ConnectionString);

            //dbConnection.Open();
            dbConnection.Open();

            OleDbCommand dbCommand = null;
            // Instantiate the Command Object
            dbCommand = new OleDbCommand(sQuery, dbConnection);
            dbCommand.CommandType = CommandType.Text;

            OleDbDataReader dr = null;
            // Execute the Stored Procedure
            dr = dbCommand.ExecuteReader();

            dtTemp = setResultSetRows(dtTemp, sChoice, dr);

            dr.Dispose();
            dbConnection.Close();

            return dtTemp;
        }

private static string setSqlQuery(string sChoice, string sFeat, string sSearch)
        {
            switch (sChoice)
            {
                case "T_PRCL":
                case "T_PRCL_FEA":
                case "T_GIS_PRCL":
                    return "SELECT * FROM " + sChoice + " WHERE " + sFeat + "='" + sSearch + "' and sys_del_flag = 0";
                case "SGD_SFC_FEAT":
                case "MSURFACE":
                case "SGD_MIN_FEAT":
                case "MMINERAL":
                    return "SELECT * FROM " + sChoice + " WHERE " + sFeat + "='" + sSearch + "' AND EXPIRY_DATE is NULL";
                case "V_SURFACE":
                case "V_MINERAL":
                    return "SELECT * FROM " + sChoice + " WHERE " + sFeat + "='" + sSearch + "'";
                default:
                    return "SELECT sysdate as UNKNOWN_ERROR from dual";
            }
        }

2. 的方法完全相同。在这里,我更愿意将所有内容放在一个名为 getResultSet 的静态方法中。此外,我确实将连接作为全局变量(OleDBConnection = null 等),连接等是自己的方法。每当连接重叠时,这给了我更多的随机错误。通常,如果一个 Reader 在另一个 Reader 之前完成。我不明白,因为它们是不同的模型(对象),他们不应该使用自己的独立变量,对象吗?!

我希望有人可以对此有所启发。

4

1 回答 1

0

在 Pabloker 的 Tipp 检查连接管理 ASP.net之后,我能够解决我的问题。

这是本准则最重要的部分。

语境

public class MyConnectionManager : IDisposable
        {
            [ThreadStatic] // static per thread
            private static OleDbConnection con;

            public static OleDbConnection Connection
            {
                get
                {
                    if (con == null)
                    {
                        con = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleDataBase"].ConnectionString);
                        con.Open();
                    }
                    return con;
                }
            }

            public void Dispose()
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

private static DataTable setResultSet(string sChoice, string sFeat, string sSearch)
        {
            DataTable dtTemp = new DataTable();
            string sQuery = setSqlQuery(sChoice, sFeat, sSearch);

            // Instantiate the Command Object
            OleDbCommand dbCommand = new OleDbCommand(sQuery, MyConnectionManager.Connection);
            dbCommand.CommandType = CommandType.Text;

            // Execute the Stored Procedure
            OleDbDataReader dr = dbCommand.ExecuteReader();

            dtTemp = setResultSetRows(dtTemp, sChoice, dr);

            return dtTemp;

    }

        public static DataTable getResultSet(string sChoice, string sFeat, string sSearch)
        {
            return setResultSet(sChoice, sFeat, sSearch);
        }

模型

public DataTable dtResultSet { get; set; }

控制器

public PartialViewResult getGrid1(string MainDDL1, string SubDDL1, string Search1)
{
    DataSearchModel voModel = new DataSearchModel();
    voModel.dtResultSet = DLA.DataSearchContext.getResultSet(MainDDL1, SubDDL1, Search1);
    return PartialView(MainDDL1, voModel);
}

public PartialViewResult getGrid2(string MainDDL2, string SubDDL2, string Search2)
{
    DataSearchModel voModel = new DataSearchModel();
    voModel.dtResultSet = DLA.DataSearchContext.getResultSet(MainDDL2, SubDDL2, Search2);
    return PartialView(MainDDL2, voModel);
}

看法

<script type="text/javascript">


    $(document).ready(function () {

        $.ajaxSetup({
            error: function (xhr, status, error) {
                alert("An AJAX error occured: " + status + "\nError: " + error);
            }
        });


        $('#btnSubmit').click(function () {
            var time = new Date().getTime(); // @* unique random number to workaround IE cache issue - IE will cache the ajax if you don't use this *@
            var oMainDDL1 = $('#MainDDL1').data("tComboBox");
            var oSubDDL1 = $('#SubDDL1').data("tComboBox");
            var sSearch1 = $("#Search1").val();

            var oMainDDL2 = $('#MainDDL2').data("tComboBox");
            var oSubDDL2 = $('#SubDDL2').data("tComboBox");
            var sSearch2 = $("#Search2").val();


            var actionURL = '@Url.Action("getGrid1", "DataSearch", new { MainDDL1 = "PLACEHOLDER" })'.replace('PLACEHOLDER', oMainDDL1.value()) + "&SubDDL1=" + oSubDDL1.value() + "&Search1=" + sSearch1 + "&time=" + time;
            if (actionURL != null) {
                alert('actionURL');
                $.get(actionURL, function (data) {
                    $('#result1').fadeOut('slow', 'linear', function () { $('#result1').empty(); $('#result1').append(data); });
                    $('#result1').fadeIn('slow', 'linear', function () {
                        if ($.browser.msie) {
                            this.style.removeAttribute('filter'); // @* Needed to fix IE7 cleartype bug with jQuery fade, but will crap out on FF/Chrome *@
                        }
                    });
                });
            }

            actionURL = '@Url.Action("getGrid2", "DataSearch", new { MainDDL2 = "PLACEHOLDER" })'.replace('PLACEHOLDER', oMainDDL2.value()) + "&SubDDL2=" + oSubDDL2.value() + "&Search2=" + sSearch2 + "&time=" + time;
            if (actionURL != null) {
                alert('actionURL');
                $.get(actionURL, function (data) {
                    $('#result2').fadeOut('slow', 'linear', function () { $('#result2').empty(); $('#result2').append(data); });
                    $('#result2').fadeIn('slow', 'linear', function () {
                        if ($.browser.msie) {
                            this.style.removeAttribute('filter'); // @* Needed to fix IE7 cleartype bug with jQuery fade, but will crap out on FF/Chrome *@
                        }
                    });
                });
            }
        });
    });

    function onMainDDL1Change(e) {
        var combo = $("#SubDDL1").data("tComboBox");
        combo.value("");
        combo.reload();
    }

    function onSubDDL1DataBinding(e) {
        var combo = $("#MainDDL1").data("tComboBox");
        e.data = $.extend({}, e.data, { mainDDL1ID: combo.value() });
    }

    function onMainDDL2Change(e) {
        var combo = $("#SubDDL2").data("tComboBox");
        combo.value("");
        combo.reload();
    }

    function onSubDDL2DataBinding(e) {
        var combo = $("#MainDDL2").data("tComboBox");
        e.data = $.extend({}, e.data, { mainDDL2ID: combo.value() });
    }
</script>

<table>
    <tr>
        <td>
            @(Html.Telerik().ComboBox()
                .Name("MainDDL1")
                .AutoFill(true)
                .DataBinding(binding => binding.Ajax().Select("LoadMainDDL", "DataSearch"))
                .HighlightFirstMatch(true)
                .ClientEvents(events => events.OnChange("onMainDDL1Change"))
                )
        </td>
        <td>
            @(Html.Telerik().ComboBox()
                .Name("MainDDL2")
                .AutoFill(true)
                .DataBinding(binding => binding.Ajax().Select("LoadMainDDL", "DataSearch"))
                .HighlightFirstMatch(true)
                .ClientEvents(events => events.OnChange("onMainDDL2Change"))
                )
        </td>
    </tr>
    <tr>
        <td>
            @(Html.Telerik().ComboBox()
                .Name("SubDDL1")
                .DataBinding(binding => binding.Ajax().Select("LoadSubDDL1", "DataSearch"))
                .HighlightFirstMatch(true)
                .ClientEvents(events => events.OnDataBinding("onSubDDL1DataBinding"))
                )
        </td>
        <td>
            @(Html.Telerik().ComboBox()
                .Name("SubDDL2")
                .DataBinding(binding => binding.Ajax().Select("LoadSubDDL2", "DataSearch"))
                .HighlightFirstMatch(true)
                .ClientEvents(events => events.OnDataBinding("onSubDDL2DataBinding"))
                )
        </td>
    </tr>
    <tr>
        <td>
            @Html.TextBox("Search1")
        </td>
        <td>
            @Html.TextBox("Search2")
        </td>
    </tr>
    <tr align="center">
        <td colspan="4">
        <input type="button" class="t-button button1" value="Search" id="btnSubmit" />
        </td>
    </tr>
</table>

<div id="result1">
</div>
<div id="result2">
</div>

局部视图 - 示例

@model ...DataSearchModel

@(Html.Telerik().Grid(Model.dtResultSet)
.Name("Grid")
.Footer(false)
.Columns(columns =>
{
columns.Bound(o => o.Row[0]).Title("T_PRCL");
}))
于 2012-07-06T17:43:22.780 回答