0

我设置了一个 MVC 母版页,它使用 AJAX 模板和 JSON 加载动态菜单 - JSON 通过以下调用返回:

var tabList = []; //this 保存回调中的 json 数据

var getTabs = function () {
    $.getJSON('Home/TabList', null, function (json) {
        Sys.Observer.clear(tabList); 
        Sys.Observer.addRange(tabList, json); 
    });
}

if (tabList == '') { getTabs(); }

所以这会在我的 HomeController 上调用一个函数来返回 JSON:

   public JsonResult  TabList()
    {
            //get tabs
            ... //call code to get tabs - removed for ease of reading this question
            JsonResult jsonTabs = Json(tabItems);

            jsonTabs.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            return jsonTabs;
        }
        catch (Exception ex)
        { throw ex; }

    }

这在以下模板中使用:绑定:

id="tabTemplate" sys:attach="dataview" class="sys-template grid_16 topnav" dataview:data="{{ tabList }}"

使用锚标签创建菜单项:

sys:href="{{'/' + 控制器 + '/' + 动作 + '/'}}" target="_top"

使用 JSON 对象中的属性填充菜单文本:

{{文本}}

然后我将此母版页附加到 2 个不同的 MVC 页面(“索引”和“关于”,global.asax 将我带到“索引”)

因此,这在我第一次进入页面时效果很好,并且菜单绘制正确,但是当我单击上面模板中生成的带有“主页/关于”链接的“关于”菜单项时,它会停止工作. 发生的事情是正确调用了javascript(即它调用“getTabs”函数并通过getJSON调用)但它实际上并没有击中我的控制器来获取JSON(我在控制器操作上放置了一个断点,它是在这种情况下不会被击中)。

我完全不知所措,几天来一直试图解决这个问题。任何帮助将不胜感激。

谢谢。

4

1 回答 1

2

I think it may be because your .getJSON request is calling the relative path Home/TabList.

When you are on the home page, e.g. http://localhost/, the .getJSON request will call http://localhost/Home/TabList which, as you say, works.

BUT then when you are on a subsequent page, e.g. http://localhost/Home/About the .getJSON request will call http://localhost/Home/About/Home/TabList which, as you say, won't work :-)

Anyway, try calling the absolute path /Home/TabList

E.g.

$.getJSON('/Home/TabList', null, function (json) {

NOTE: The extra forward slash at the beginning.

HTHs,
Charles

于 2009-12-17T00:41:58.427 回答