如何在 Jquery 中访问控制器操作结果?

发布于 2024-12-04 03:11:50 字数 920 浏览 1 评论 0原文

我正在编写多级菜单的代码。主菜单,即一级菜单在页面加载时加载。单击主菜单中的项目后,将创建子级菜单。 sulevel 菜单中的项目是由控制器中的操作结果创建的。因为它是 n 级子菜单,所以我使用 jquery 来填充子菜单。

以下是主菜单的代码,它是一个简单的列表。

    <ul>
    <li><a href="#" >item1</a>
    <li><a href="#">item2</a> </li>
    <li><a href="#">item3</a></li>
    <li><a href="#">item4 </a></li>
    </ul>

以下是控制器的操作结果,该控制器返回给定 ID 的菜单项的子项。

     public PartialViewResult SubItems(int id)
    {

            MyQueries obj = new MyQueries();
            ViewData["SubCategories"] = obj.getChildCategories(id);
            ViewData["Services"] = obj.getServices(id);
            return PartialView();


    }

我希望上面的细节能给你一个想法,我想编写一个jquery代码,一个锚点的点击事件..它接收作为项目文本的参数并使用控制器并创建子菜单。 例如,用户单击 item1,jquery 代码应将“item1”id 发送到返回子项的控制器。然后代码应创建包含子项的 UL lI 列表。我希望我说清楚了

I am writing a code for multilevel menu. Main menu ie first level menu is loadedin page load. Upon clicking the item in the main menu sublevel menu are created. Items in the sulevel menu is created by action result in controller. Since it is n level submenu I am using jquery to populate submenu.

Following is the code of main menu, which is a simple list

    <ul>
    <li><a href="#" >item1</a>
    <li><a href="#">item2</a> </li>
    <li><a href="#">item3</a></li>
    <li><a href="#">item4 </a></li>
    </ul>

Following is the action result of a controller which returns the subitems of menu item given it's ID.

     public PartialViewResult SubItems(int id)
    {

            MyQueries obj = new MyQueries();
            ViewData["SubCategories"] = obj.getChildCategories(id);
            ViewData["Services"] = obj.getServices(id);
            return PartialView();


    }

I hope above details gives u an idea, I want to write a jquery code, a click event for anchor.. which receives the parameter that is item text and uses the controller and creates submenu.
For example user clicks on item1, jquery code should send 'item1' id to a controller which returns child items.. then code should create UL lI list which has subitems. I hope I made it clear

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2024-12-11 03:11:50

我将使用 jquery ajax 执行 get 请求,并确保指定它从服务器接受的数据类型:

$('#anchorid').click(function() {
var returnData = $.ajax({ url: @Url.Action("subitems"),
type: "GET",
dataType: "json",
data: {"id": this.attr("id"),
});
//TODO:  build your list using returnData object

});

此代码远非完美,但应该让您了解如何使用 Ajax 返回请求。请注意,如果您要使用此技术,则不应在服务器上返回部分视图,而应返回 AJAX 期望的任何对象类型。以下是 jQuery AJAX 请求的参考资料: http://api.jquery.com/jQuery.ajax/

I would use jquery ajax to perform a get request, and be sure to specify what datatype it accepts from the server:

$('#anchorid').click(function() {
var returnData = $.ajax({ url: @Url.Action("subitems"),
type: "GET",
dataType: "json",
data: {"id": this.attr("id"),
});
//TODO:  build your list using returnData object

});

This code is far from perfect, but should give you an idea on how to use Ajax to return a request. Note that if you're going to use this technique, you shouldn't be returning a partial view on the server, you should be returning whatever object type the AJAX is expecting. Heres the reference material for the jQuery AJAX request: http://api.jquery.com/jQuery.ajax/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文