0

我在 html 中有一个网格类型的 div,我在其中做一些记录。如果单击编辑,将打开一个弹出窗口,用户可以编辑记录,数据将保存到数据库中。保存数据后,我的网格应该刷新。

我们使用 ajax 对 mvc 控制器的所有这些调用,但网格没有刷新。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 回答