0

MVC 4 アプリケーションでは、リンクをクリックすると、関連する製品リストがライトボックスに表示されます。私は必要なメソッドリターン製品を持っています:

 public ActionResult GetRelatedProducts(int id)
    {
       var realProducts = GetRelatedProducts(id);
       List<object> productsObjectList = new List<object>();
       foreach (var item in realProducts)
       {
             productsObjectList .Add(new
             {
                 id = item.Id,
                 fullname = item.Name
             });
       }
       return Json(productsObjectList , JsonRequestBehavior.AllowGet);
    }

HTML は次のとおりです。

<a class="show" id="show">Show</a>

<div  id="productBox" style="display: none;">
    // Product list will get here
</div>

そしてスクリプト:

     $('#show').click(function (e) {
     url = '@Url.Action("GetRelatedProducts", "Product")';
     var data = { id: '@Model.Id' };
     $.post(url, data, function (result) {
           $('#productBox').lightbox_me({
           onLoad: function () {

             //How to send returned product list to light box, to show them by foreach loop

           }
       });
      e.preventDefault();
  });
});

製品リストを送信して製品を表示するにはどうすればよい productBoxですか?

4

1 に答える 1

2

あなたがコーディングする:

$('#show').click(function (e) {
     url = '@Url.Action("GetRelatedProducts", "Product")';
     var data = { id: '@Model.Id' };
     $.post(url, data, function (result) { // <- "result" will contain array
       $('#productBox').lightbox_me({
           onLoad: function () { ... }
       });
       e.preventDefault(); // <- this must prevent "a" tag, put it outside
     });
});

次のように、クライアント側でリストを使用できます。

$.post(url, data, function (result) {
    var list = '<ul>';
    for(var i = 0; i < result.length; i++)
    {
        list += '<li>' + result[i].fullname + '</li>';
    }
    list += '</ul>';
    $('#productBox').html(list).lightbox_me();
});

または、Vladimir Bozicが書いたようPartialViewResultに、コントローラーリターンから、を使用しますPartialView。これは通常のビューと同じですが、レイアウトがなく、htmlブロックだけで、次のように使用できます。

$.post(url, data, function (result) {
    $('#productBox').html(result).lightbox_me();
});
于 2013-02-22T14:12:58.237 に答える