在装载点击同一页面上的href链接局部视图 [英] Load partial View on clicking href link on the same page

查看:138
本文介绍了在装载点击同一页面上的href链接局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须返回一个强类型的局部视图与产品列表动作

 公众的ActionResult的GetProducts(INT CATID,诠释LANGID)
        {
            CategoryViewModel OB =新CategoryViewModel();
            ob.GetProducts(CATID,LANGID);
            返回PartialView(的GetProducts,OB);        }

在我的指数()查看有关文件准备好我加载它返回我一个divcategoriesPlaceGetCategories行动的局部视图。

这里是我的指数()查看

 <脚本>
    $(文件)。就绪(函数(){        $(#categoriesPlace)的负载('?LANGID ='。'@ Url.Action(GetCategories)'+ + $(#ddlLanguages​​)VAL());
    });
< D​​IV ID =categoriesPlace>< / DIV>
< D​​IV ID =productsPlace>< / DIV>


在GetCategories()查看我有很多的链接。 所以我想,当我点击一些这个链接 - 加载DIV productsPlace(这是在指数()视图)与从的GetProducts()动作

返回的局部视图

这里是我的GetCategories()查看

  @model IEnumerable的< OnlineStore.Commercial.Models.CategoryViewModel>@ {
    ViewBag.Title =指数;
}
    @foreach(在@Model VAR树)
    {
    < UL ID =树>
        <立GT;
            < A HREF =@ Url.Action(的GetProducts,新的{CATID = tree.Category code,LANGID = 1})> @ tree.CategoryName< / A>        < /李>
    < / UL>
    }


解决方案

我会给所有的< A> 元素中的一类,说的getProducts ,然后绑定一个click事件对他们说:
(请原谅我以书面形式URL中的<%语法)

因此​​,对于< A> 元素:

 < A HREF =JavaScript的:无效(0);类=的getProducts> @ tree.CategoryName&下; / A>

和click事件绑定:

  $('的getProducts')。在('点击',功能(){
     $('#productsPlace')负载('<%= Url.Action(的GetProducts,YourController)%>',新的{CATID = tree.Category code,LANGID = 1},空);
});

SO I have action which returns a strongly typed partial view with list with products

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

        }

In my Index() view on document ready I load the partial view which returns me GetCategories action in a div "categoriesPlace".

Here is my Index() View

<script>


    $(document).ready(function () {

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


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


In GetCategories() view I have a lot of links. SO I WANT WHEN I CLICK SOME OF THIS LINK - to load the div productsPlace(which is in index() view) with the partial view which is returned from GetProducts() Action

Here is my GetCategories() View

@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>
    }

解决方案

I would give all the <a> elements a class, say getProducts, and then bind a click event to them: (Forgive me for writing the url in the <% syntax)

So for the <a> elements:

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

And for the click event binding:

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

这篇关于在装载点击同一页面上的href链接局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆