通过单击链接将数据发送到模态窗口

时间:2012-08-10 18:45:30

标签: jquery asp.net-mvc-3

我当然有以下代码,它可以调用ajax来控制器工作正常。

@Ajax.ActionLink("Add to Wishlist", "Add", "Wishlist", new { productId = Product.ProductId }, new AjaxOptions { UpdateTargetId = "breadcrumbs", InsertionMode = InsertionMode.Replace, HttpMethod = "Post" }, new { @class = "button" })

现在我需要更改此代码并允许用户同时添加一些额外数据以及产品ID。

现在要求在单击“添加到心愿单”链接时打开模态弹出窗口,并在模态窗口中显示下拉列表和文本框,一旦用户填写它们并单击模态弹出窗口中的提交按钮,然后发送数据到控制器。

现在,我的网页有多个像这样的链接,所有链接都有不同的productId。

我的第一个问题如何将产品ID发送到模态弹出窗口。

由于 阿纳布

2 个答案:

答案 0 :(得分:0)

在主视图中(从中启动模式):

TempData["ProdId"] = <your product id>

在你的模态(或弹出)对话框中:

var ProdId = (int)TempData["ProdId"]

希望这有帮助。

答案 1 :(得分:0)

使用Html.ActionLink辅助方法

切换到普通链接
@Html.ActionLink("Add to Wishlist", "Add", "Wishlist",
                           new { @id=product.ProductId},new { class="modelLink"})

现在做一些javascript来听这个链接的click事件,然后打开Dialog并发送你想要的任何参数。下面的代码是打开一个模型弹出窗口(jQuery UI Dialog(这意味着你需要包含jQuery UI库才能使这个代码工作))并且它正在向Add动作方法发送2个参数WishList 1}}控制器。无论您从Add操作方法返回什么,都将显示在Model对话框中。

$(function(){    
  $('a.modelLink').click(function(){

     var item=$(this);

     var url=item.attr("href")+"&anotherParam=unicorn";

        var dialog = $("#dialog");
        if ($("#dialog").length == 0) {
            dialog = $('<div id="dialog" style="display:hidden"></div>').appendTo('body');
        }
        dialog.load(
            url,
            {}, // omit this param object to issue a GET request instead a POST request, otherwise you may provide post parameters within the object
            function (responseText, textStatus, XMLHttpRequest) {
                dialog.dialog({                       
                    close: function (event, ui) {                            
                        dialog.remove();
                    },
                    modal: true,
                    width: 460
                });
            }
        );
    }); 

});
相关问题