rohit rohit - 2 months ago 6
Ajax Question

get different data from mvc action method on ajax call for two different button clicks

i want the functionality that when i click on withPricing button ajax call must get withPricing template from action method & when i click without pricing ajax must get withoutPricing template from action method.

i have two action links :

<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>


onclick of any of these buttons following ajax action gets called :

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;
}


in response this ajax method gets data from my mvc controller's action method
which is as follows :

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);
}


i want the functionality that when i click on withPricing button ajax call must get withPricing template from action method & when i click without pricing ajax must get withoutPricing template from action method.

how can i achieve this?

Answer

you can use one more parameter to differentiate your call.

    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;
}

so your action changes to :

    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);
    } 

and html will be:

<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>
Comments