javascript - 对于两次不同的按钮点击,从 Ajax 调用的 MVC 操作方法中获取不同的数据

标签 javascript ajax asp.net-mvc-4

我想要的功能是,当我单击 withPricing 按钮时,ajax 调用必须从操作方法中获取 withPricing 模板,而当我单击但没有定价时,ajax 必须从操作方法中获取 withoutPricing 模板。

我有两个操作链接:

<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 操作之后调用任何这些按钮的 onclick :

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 Controller 的操作方法获取数据 如下:

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 调用必须从操作方法中获取 withPricing 模板,而当我单击但没有定价时,ajax 必须从操作方法中获取 withoutPricing 模板。

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

最佳答案

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

    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>

关于javascript - 对于两次不同的按钮点击,从 Ajax 调用的 MVC 操作方法中获取不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39764028/

相关文章:

javascript - 如何以简单的方式创建数据表?

php - 使用ajax将JavaScript数组传输到PHP数组?

javascript - 处理ajax返回值

c# - 如何在 Entity Framework 代码优先方法中映射自身的递归关系

jquery - ASP.NET MVC 4 - 客户端验证不起作用

javascript - highchart 无法添加系列

javascript - 使用 Javascript 淡入和淡出 CSS 样式表

javascript - 字符串中的所有字符都必须匹配正则表达式

java - 如何使用 Spring Boot Async 提交表单而不重新加载页面?

asp.net-mvc-4 - MVC WebApi - System.MissingMethodException : Method not found: 'Void System.Net.Http.ObjectContent