在同一页面上单击href链接时加载部分视图

时间:2014-02-12 06:34:02

标签: asp.net-mvc asp.net-mvc-partialview

所以我有一个动作,它返回带有产品列表的强类型部分视图

public ActionResult GetProducts(int catID,int langID)
        {
            CategoryViewModel ob = new CategoryViewModel();
            ob.GetProducts(catID, langID);
            return PartialView("GetProducts",ob);

        }

在文档就绪的Index()视图中,我加载了部分视图,该视图在div“categoriesPlace”中返回了GetCategories操作。

这是我的索引()视图

<script>


    $(document).ready(function () {

        $("#categoriesPlace").load('@Url.Action("GetCategories")' + '?langId=' + $("#ddlLanguages").val());


    });
<div id="categoriesPlace"></div>
<div id="productsPlace"></div>

在GetCategories()视图中,我有很多链接。 我想点击这个链接的一些内容 - 使用从GetProducts()返回的部分视图加载div productsPlace(在index()视图中)Action

以下是我的GetCategories()视图

@model IEnumerable<OnlineStore.Commercial.Models.CategoryViewModel>

@{
    ViewBag.Title = "Index";
}




    @foreach ( var tree in @Model)
    {
    <ul id="tree">
        <li>
            <a href="@Url.Action("GetProducts",  new { catID = tree.CategoryCode,langID= 1})">@tree.CategoryName</a>

        </li>
    </ul>
    }

1 个答案:

答案 0 :(得分:1)

我会给所有<a>元素一个类,比如getProducts,然后将click事件绑定到它们: (请原谅我用<%语法编写网址)

对于<a>元素:

<a href="javascript:void(0);" class="getProducts">@tree.CategoryName</a>

对于点击事件绑定:

$('.getProducts').on('click', function() {
     $('#productsPlace').load('<%=Url.Action("GetProducts", "YourController")%>', new { catID =  tree.CategoryCode,langID= 1}, null);
});