MVC $.getJSON 第一次在母版页上工作,然后在后续加载时停止调用 url
我设置了一个 MVC 母版页,它使用 AJAX 模板和 JSON 加载动态菜单 - JSON 通过以下调用返回:
var tabList = []; //这在回调中保存了 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="{{ '/' + Controller + '/' + Action + '/'}}" target="_top"
使用 JSON 对象中的属性填充菜单文本:
{{Text}}
然后我将此母版页附加到 2 个不同的 MVC页面(“索引”和“关于”,以及 global.asax 将我带到“索引”)
因此,当我第一次进入页面时,这非常有效,并且菜单绘制正确,但是当我单击“关于”菜单时在上述模板中生成的带有“主页/关于”链接的项目将停止工作。发生的情况是,javascript 被正确调用(即它调用“getTabs”函数并通过 getJSON 调用),但它实际上并没有击中我的控制器来获取 JSON(我在控制器操作上放置了一个断点,它是在这种情况下不会被击中)。
我完全不知所措,几天来一直在努力解决这个问题。任何帮助将不胜感激。
谢谢。
I have an MVC masterpage set up which loads a dynamic menu using AJAX Templates and JSON - the JSON is returned with the following call:
var tabList = []; //this holds the json data on the callback
var getTabs = function () {
$.getJSON('Home/TabList', null, function (json) {
Sys.Observer.clear(tabList);
Sys.Observer.addRange(tabList, json);
});
}
if (tabList == '') { getTabs(); }
So this calls a function on my HomeController to return 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; }
}
This is used in the following template:
binding:
id="tabTemplate" sys:attach="dataview" class="sys-template grid_16 topnav" dataview:data="{{ tabList }}"
creating the menu items using an anchor tag:
sys:href="{{'/' + Controller + '/' + Action + '/'}}" target="_top"
Populating the menu text using a property from the JSON object:
{{Text}}
I then attached this master page to 2 different MVC pages ("Index" and "About", and global.asax takes me to "Index")
So, this works great the first time I go in to the page, and the menu is drawn correctly, but when I click the About menu item which has been generated in the above template with the link "Home/About" then it stops working. What happens is that the javascript is called correctly (i.e. it calls the "getTabs" function and passes through the getJSON call) BUT it doesn't actually hit my controller to get the JSON (I put a brekpoint on the controller action and it is not hit in this case).
I am totally at a loss and have been trying to solve this for days. Any help would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能是因为您的 .getJSON 请求正在调用相对路径 Home/TabList 。
当您位于主页时,例如
http://localhost/
,.getJSON
请求将调用http://localhost/Home/TabList
code> 正如你所说,它有效。但是,当您进入后续页面时,例如
http://localhost/Home/About
,.getJSON
请求将调用http://localhost/Home /About/Home/TabList
正如你所说,它不会工作:-)无论如何,尝试调用绝对路径
/Home/TabList
例如
注意:额外的正斜杠开始。
HTH,
查尔斯
I think it may be because your
.getJSON
request is calling the relative pathHome/TabList
.When you are on the home page, e.g.
http://localhost/
, the.getJSON
request will callhttp://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 callhttp://localhost/Home/About/Home/TabList
which, as you say, won't work :-)Anyway, try calling the absolute path
/Home/TabList
E.g.
NOTE: The extra forward slash at the beginning.
HTHs,
Charles