0

私はいくつかのレコードを作成している html にグリッドのような div を持っています。編集をクリックすると、ポップアップが開き、ユーザーはレコードを編集でき、データはデータベースに保存されます。データを保存すると、グリッドが更新されます。

これらの mvc コントローラーへの呼び出しはすべて ajax を使用して実行していますが、グリッドは更新されません。ajay の投稿にいくつかの問題があります。「refreshRules」の成功メソッドが起動されることはありません。「saveMetricRule」、「metricruleeditor」などの他のアクションでは正常に機能します。

誰かが私がここで間違っていることを教えてもらえますか?

以下はjqueryコードブロックです:

var m_oConfig = {

        editMetricRule: {
            ds: "/stealth/metricruleeditor",
            p: function (config, elem) { return YAHOO.stealth.editMetricRuleP(config, elem); },
            s: function (axn, config, elem, data, textStatus, jqXHR) { YAHOO.stealth.editRuleS(axn, config, elem, data, textStatus, jqXHR); }
        },

        saveMetricRule: {
            ds: "/stealth/saveMetricRule",
            p: function (config, elem) { return YAHOO.stealth.saveMetricRuleP(config, elem); },


            s: function (axn, config, elem, data, textStatus, jqXHR) { YAHOO.stealth.saveMetricRuleS(axn, config, elem, data, textStatus, jqXHR); }
        },

        refreshRules: {
            ds: "/stealth/metricsrefresh",
            p: function (config, elem) { return YAHOO.stealth.refreshRulesP(config, elem); },
            s: function (axn, config, elem, data, textStatus, jqXHR) { YAHOO.stealth.refreshRulesS(axn, config, elem, data, textStatus, jqXHR); }
        }

refreshRulesP: function (config, elem) {
        return {
            gid: $(elem).attr("data-gid"),
        };
    },
    refreshRulesS: function (axn, config, elem, data, textStatus, jqXHR) {
        var sRoot = "rules";

        $("#" + sRoot).replaceWith(data);

        YAHOO.stealth.bindMetricGrids(sRoot);
    },


    saveMetricRuleP: function (config, elem) {
        var sErrMsg = "There are errors in the rule.";

        //rErr class added during any change
        if ($(".rErr").length) {
            //update rule pop-in
            YAHOO.stealth.ErrHndlr(sErrMsg, "pnlRuleError");

            //cancel ajax (and update actual page)
            throw "";
        }

        //return the rule
        return {
            rule: YAHOO.stealth.getRuleObj()
        };
    },

    saveMetricRuleS: function (axn, config, elem, data, textStatus, jqXHR) {
        //close the pop-in
        CSUtils.DisablePop();

        //refresh the rules
        YAHOO.stealth.loadNaked("refreshRules", null, elem);

        //Indicate to user that they must run the rules
        YAHOO.stealth.needRuleRun(data.msg, elem);
    },


 loadNaked: function (axn, config, elem) {
        if (BlockAjax
            || (!axn || axn == "")) { return; }

        var oAxn = m_oConfig[axn];

        try {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: oAxn.ds,
                data: JSON.stringify(oAxn.p(config, elem)),
                success: function (data, textStatus, jqXHR) {
                    alert('s');
                    //if they defined a success function (s), call it with all the init and return data
                    if (typeof oAxn.s === "function") {
                        oAxn.s(axn, config, elem, data, textStatus, jqXHR)
                    }

                    //any dependent actions need to be called as well
                    if (oAxn.deps && oAxn.deps.length > 0) {
                        $.each(oAxn.deps, function (idx, dep) {
                            YAHOO.stealth.loadNaked(dep, config, elem);
                        });
                    }
                },
                error: function (err, type, msg) {
                    YAHOO.stealth.ErrHndlr(err.responseText);
                }
            });
        } catch (err) {

            if (err && err !== "") {
                YAHOO.stealth.ErrHndlr(err.responseText);
            }

            BlockAjax = false; //if it was set, we should unset it
        }
    },

コントローラーのアクション:

  public ActionResult metricsrefresh(int gid)
    {
        UIGrid oGrid = this.metricRulesGrid(gid);

        string myString = RenderViewToString(this.ControllerContext , MVCConstants.VIEW_LISTABLE, this.metricRulesGrid(gid));

        return this.Json(new
        {
            myString 
        });

    }


    public ActionResult saveMetricRule(Rule rule)
    {
        bool IsNew = rule.RuleId  == 0;

        using (NewAngieDataContext oAngieCtxt = new NewAngieDataContext(new CSConfigurationMgr().GetConnectionString(ConnectionStringKey.Angie)))
        {
            if (IsNew)
                oAngieCtxt.Rules.InsertOnSubmit(rule);
            else
            {
                RuleCondition oRuleCon = null;
                foreach (RuleCondition childItem in rule.RuleConditions)
                {
                    oRuleCon =
                           oAngieCtxt.RuleConditions
                                .Where(CON => CON.RuleConditionId == childItem.RuleConditionId )
                                .FirstOrDefault();

                    oRuleCon.Points = childItem.Points;
                    oRuleCon.ConditionValue = childItem.ConditionValue;
                    oRuleCon.ToOperatorId = childItem.ToOperatorId;
                    oRuleCon.Sort = childItem.Sort;
                }

                oAngieCtxt.Rules.Attach(rule);
                oAngieCtxt.Refresh(RefreshMode.KeepCurrentValues, rule);
            }
            oAngieCtxt.SubmitChanges();
        }

        return this.Json(new
        {
            msg = "Successful save.",
            ruleId = rule.RuleId
        });
    }
4

1 に答える 1

0

これがローカルホストではなく本番環境で機能している場合は、データを投稿している場所の URL が、サイトのルートに対して相対的ではなく、絶対的であることを示しています (詳細は提案されたソリューションの下にあります)。

絶対 URL を、サーバー上で動的に作成された URL に置き換えてみてください。つまり、JavaScript でこれの代わりに:

editMetricRule: {
        ds: "/stealth/metricruleeditor",

このようなことを試してください:

editMetricRule: {
        ds: @Url.Action("metricruleeditor", "stealth"),

とにかくこれを行うのは良い習慣です;)


詳細:

したがって、ルートが存在する可能性のあるサーバーで機能します。

http://myserver.com/

したがって、これは次のように/stealth/metricruleeditorなります。

http://myserver.com/stealth/metricruleeditor

ただし、ローカルホストでは、ルートは次のようになります。

http://localhost/myAppName

データを投稿するための正しい URL は次のとおりです。

http://localhost/myAppName/stealth/metricruleeditor

ただし、JS の「/stealth/metricruleeditor」は次のように変換されます。

http://localhost/stealth/metricruleeditor

これは、ブラウザーで開発ツールを見て、ネットワーク トラフィック (要求とサーバーの応答) を監視することで確認できます。

于 2015-12-04T15:45:17.317 に答える