我想要的功能是,当我单击 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/