weixin_33713503 2014-11-11 08:54 采纳率: 0%
浏览 29

MVC异步Ajax调用

I'm working on an eCommerce website and I have the following template:

<div class="product-box">
    Product 1
</div>

<div class="product-box">
    Product 2
</div>

<div class="product-box">
    Product 3
</div>

The website is ASP.NET MVC, I want to fill the different product box with product information asynchronously using jQuery ajax calls to my MVC controller, what is the best way to do it?

I can already think of something like that

<div id="product1" class="product-box">
    Product 1
</div>

<div id="product2" class="product-box">
    Product 2
</div>

<div id="product3" class="product-box">
    Product 3
</div>

<script>
$.get("/GetProductInfo/1", {}, function (data) {
    $("#product1").html(data);
});

$.get("/GetProductInfo/2", {}, function (data) {
    $("#product2").html(data);
});

$.get("/GetProductInfo/3", {}, function (data) {
    $("#product3").html(data);
});
</script>

But this doesn't make sense and is not the best way to do it. Imagine I have 1000 products, madness. Anybody can advice the best way I can achieve this?

  • 写回答

2条回答 默认 最新

  • weixin_33691817 2014-11-11 09:02
    关注

    you can try this:

    HTML:

    you can use data-* attribute to provide product numbers like:

    <div data-id="1" class="product-box">
    <div data-id="2" class="product-box">
    <div data-id="3" class="product-box">
    .......
    // more
    

    jQuery:

    $(function(){
        $('.product-box').each(function(){
            var dataID = $(this).data('id');
            $.get("/GetProductInfo/" + dataID, {}, function (data) {
                 $("#product"+dataID).html(data);
            });
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services