从ajax调用获取mvc操作方法的不同数据,获得两次不同的按钮点击

时间:2016-09-29 07:29:16

标签: javascript ajax asp.net-mvc-4

我想要的功能是,当我点击withPricing按钮时,ajax调用必须从action方法&获取withPricing模板当我没有定价点击时,ajax必须从动作方法中获得不带定价的模板。

我有两个动作链接:

<html>
<body> 
<a id="print" name="withPrice" style="text-decoration:none;" onclick="callPrint('@item.Id');</a>

<a id="print" name="withoutPrice" style="text-decoration:none;" onclick="callPrint('@item.Id');</a>
</html>
</body>

在调用ajax动作后点击任何这些按钮:

function callPrint(item) {
        var PrintCssContent = "";

        $.ajax({
            type: "GET",
            url: '@Url.Action("GetHtmlString", "Itinerary", new { area = "Travel"})?tripid=' + item,
            //data: item,
            dataType: "text",
            success: function (data) {
                var parseData = JSON.parse(data);
                alert(parseData.html);

                var WinPrint =
                window.open('', '', 'width=auto,height=auto,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');

                WinPrint.document.write(parseData.html);
                WinPrint.print();
            },
            error:function(){
                alert('error');
            }
        });
        return false;
    }

作为回应,这个ajax方法从我的mvc控制器的action方法中获取数据 如下:

public ActionResult GetHtmlString(long tripId)
{
string templateNameWithPricing =ConfigurationSettings.AppSettings["EmailTemplateBaseDirectoryWithPricing"].ToString();

string templateNameWithoutPricing = ConfigurationSettings.AppSettings["EmailTemplateBaseDirectoryWithoutPricing"].ToString();

tripService.SendPurchasedEmailForSpirit(objTravel, templateNameWithoutPricing, siteContext, ref htmlString, false, false, false, false, user);

tripService.SendPurchasedEmailForSpirit(objTravel, templateNameWithPricing, siteContext, ref htmlString, false, false, false, false, user);

return Json(new { html = htmlString }, JsonRequestBehavior.AllowGet);
}

我想要的功能是,当我点击withPricing按钮时,ajax调用必须从action方法&amp;获取withPricing模板当我没有定价点击时,ajax必须从动作方法中获得不带定价的模板。

我怎样才能实现这一目标?

1 个答案:

答案 0 :(得分:3)

您可以再使用一个参数来区分您的通话。

    function callPrint(item, withOrwithout) {
    var PrintCssContent = "";

    $.ajax({
        type: "GET",
        url: '@Url.Action("GetHtmlString", "Itinerary", new { area = "Travel"})?tripid=' + item + '&withOrwithout=' + withOrwithout,
        //data: item,
        dataType: "text",
        success: function (data) {
            var parseData = JSON.parse(data);
            alert(parseData.html);

            var WinPrint =
            window.open('', '', 'width=auto,height=auto,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');

            WinPrint.document.write(parseData.html);
            WinPrint.print();
        },
        error:function(){
            alert('error');
        }
    });
    return false;
}

所以你的行动改为:

    public ActionResult GetHtmlString(long tripId, string withOrwithout)
    {
        string templateName = ConfigurationSettings.AppSettings["EmailTemplateBaseDirectoryWithoutPricing"].ToString();

        if (withOrwithout == "withPrice")
            templateName = ConfigurationSettings.AppSettings["EmailTemplateBaseDirectoryWithPricing"].ToString();

        tripService.SendPurchasedEmailForSpirit(objTravel, templateName, siteContext, ref htmlString, false, false, false, false, user);

        return Json(new { html = htmlString }, JsonRequestBehavior.AllowGet);
    } 

和html将是:

<html>
<body> 
<a id="print" name="withPrice" style="text-decoration:none;" onclick="callPrint('@item.Id', 'withPrice');</a>

<a id="print" name="withoutPrice" style="text-decoration:none;" onclick="callPrint('@item.Id', 'withoutPrice');</a>
</html>
</body>