打印模型数据而不保存
Print model data without saving
if (saleDetails.length) {
var htmlData;
var paymentStatus = 0;
if ($('#PaymentStatus option:selected').val() != 0) {
paymentStatus = $('#PaymentStatus option:selected').text()
}
var SaleAmount = parseFloat(total + vat).toFixed(2);
var data = {
'AccountID': $('#hdnAccountID').val(),
'QuoteID': $('#hdnQuoteID').val(),
'BranchID': $('#BranchID option:selected').val(),
'PONO': $('#PONO').val(),
'PaymentStatus': $('#PaymentStatus').val(),
'SalesDate': $('#SaleDate').val(),
'PaymentStatus': paymentStatus,
'PaymentTypeID': $('#PaymentType option:selected').val(),
'VAT': vat,
'TotalAmount': invoiceAmount,
'DiscountAmount': $('#discInput').val(),
'AmountPaid': $('#amountPaid').val(),
'SaleDetails': saleDetails
};
var json = JSON.stringify({ 'model': data });
public ActionResult printOrder(Models.DTO.Sales model)
{
return PartialView(model);
//return View(model);
}
我在做POS,在销售客户的要求是我们应该给他一个打印选项,这样如果客户点击打印按钮我们应该打开一个新标签并显示发票,这样客户就可以打印出来如果客户付钱给他,那么客户将保存 SalesOrder。
我面临的问题是我无法从 controller 打开新选项卡。如果我试图从 java 脚本执行此操作,我将无法传递模型以从 java 脚本查看。
所以请帮助我解决这个问题,因为我不是 MVC 的专家。
您可以使用 Html.ActionLink 从您的 Razor 页面在新选项卡中打开页面,如下所示。
@Html.ActionLink("Print", "Action", new { controller="PrintOrder" }, new { target="_blank" })
Html.ActionLink
但是不允许您传递复杂的对象。您可以使用此 Whosebug 答案中提到的技巧来通过您的模型。来自 post:
MODEL: Make static Serialize and Deserialize methods in the class like
public class XYZ {
// Some Fields
public string X { get; set; }
public string Y { get; set; }
public string X { get; set; }
// This will convert the passed XYZ object to JSON string
public static string Serialize(XYZ xyz)
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(xyz);
}
// This will convert the passed JSON string back to XYZ object
public static XYZ Deserialize(string data)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<XYZ>(data);
}
}
VIEW: Now convert your complex object to JSON string before passing it in
Action View <%= Html.ActionLink(Model.x, "SomeAction", new { modelString = XYZ.Serialize(Model) })%>
CONTROLLER: Get the
object as string in Action method and convert it back to object before
using public ActionResult SomeAction(string modelString) { XYX xyz = XYX.Deserialize(modelString); }
if (saleDetails.length) {
var htmlData;
var paymentStatus = 0;
if ($('#PaymentStatus option:selected').val() != 0) {
paymentStatus = $('#PaymentStatus option:selected').text()
}
var SaleAmount = parseFloat(total + vat).toFixed(2);
var data = {
'AccountID': $('#hdnAccountID').val(),
'QuoteID': $('#hdnQuoteID').val(),
'BranchID': $('#BranchID option:selected').val(),
'PONO': $('#PONO').val(),
'PaymentStatus': $('#PaymentStatus').val(),
'SalesDate': $('#SaleDate').val(),
'PaymentStatus': paymentStatus,
'PaymentTypeID': $('#PaymentType option:selected').val(),
'VAT': vat,
'TotalAmount': invoiceAmount,
'DiscountAmount': $('#discInput').val(),
'AmountPaid': $('#amountPaid').val(),
'SaleDetails': saleDetails
};
var json = JSON.stringify({ 'model': data });
public ActionResult printOrder(Models.DTO.Sales model)
{
return PartialView(model);
//return View(model);
}
我在做POS,在销售客户的要求是我们应该给他一个打印选项,这样如果客户点击打印按钮我们应该打开一个新标签并显示发票,这样客户就可以打印出来如果客户付钱给他,那么客户将保存 SalesOrder。 我面临的问题是我无法从 controller 打开新选项卡。如果我试图从 java 脚本执行此操作,我将无法传递模型以从 java 脚本查看。 所以请帮助我解决这个问题,因为我不是 MVC 的专家。
您可以使用 Html.ActionLink 从您的 Razor 页面在新选项卡中打开页面,如下所示。
@Html.ActionLink("Print", "Action", new { controller="PrintOrder" }, new { target="_blank" })
Html.ActionLink
但是不允许您传递复杂的对象。您可以使用此 Whosebug 答案中提到的技巧来通过您的模型。来自 post:
MODEL: Make static Serialize and Deserialize methods in the class like
public class XYZ { // Some Fields public string X { get; set; } public string Y { get; set; } public string X { get; set; } // This will convert the passed XYZ object to JSON string public static string Serialize(XYZ xyz) { var serializer = new JavaScriptSerializer(); return serializer.Serialize(xyz); } // This will convert the passed JSON string back to XYZ object public static XYZ Deserialize(string data) { var serializer = new JavaScriptSerializer(); return serializer.Deserialize<XYZ>(data); } }
VIEW: Now convert your complex object to JSON string before passing it in
Action View <%= Html.ActionLink(Model.x, "SomeAction", new { modelString = XYZ.Serialize(Model) })%>
CONTROLLER: Get the object as string in Action method and convert it back to object before
using public ActionResult SomeAction(string modelString) { XYX xyz = XYX.Deserialize(modelString); }