ajax jquery帖子不在mvc工作

时间:2013-08-30 06:18:37

标签: c# asp.net-mvc jquery

我在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
        });
    }

1 个答案:

答案 0 :(得分:0)

如果这适用于生产而不是本地主机,则表明您发布数据的URL是绝对的,而不是相对于站点根目录(详细信息如下所示)。

您可以尝试使用服务器上动态创建的URL替换绝对URL。即而不是在你的JavaScript中:

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

尝试这样的事情:

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

无论如何,这是一个很好的做法;)

<强>详细信息:

因此,它适用于您的root用户所在的服务器:

http://myserver.com/

所以,这个/stealth/metricruleeditor变为:

http://myserver.com/stealth/metricruleeditor

但是,在localhost上你的root可能是:

http://localhost/myAppName

以及发布数据的正确网址为:

http://localhost/myAppName/stealth/metricruleeditor

但是,&#34; / stealth / metricruleeditor&#34;在您的JS中转换为:

http://localhost/stealth/metricruleeditor

您可以通过查看浏览器中的开发工具并监控网络流量来确认:请求和服务器响应。