【问题标题】:Print model data without saving打印模型数据而不保存
【发布时间】:2019-05-26 18:00:55
【问题描述】:
 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。 我面临的问题是我无法从控制器打开新标签。如果我试图从 java 脚本中执行此操作,我无法将模型传递给从 java 脚本中查看。 所以请在这个问题上帮助我,因为我不是 MVC 方面的专家。

【问题讨论】:

  • 您可以简单地创建一个操作来呈现视图上的数据并允许用户打印当前页面。

标签: javascript c# asp.net-mvc-5


【解决方案1】:

您可以使用Html.ActionLink 在 Razor 页面的新标签页中打开页面,如下所示。

@Html.ActionLink("Print", "Action", new { controller="PrintOrder" }, new { target="_blank" })

Html.ActionLink 但是不允许您传递复杂的对象。您可以使用此stackoverflow 答案中提到的技巧来传递您的模型。来自帖子:

MODEL:在类中制作静态 Serialize 和 Deserialize 方法,如

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

查看:现在在传入复杂对象之前将其转换为 JSON 字符串

Action View <%= Html.ActionLink(Model.x, "SomeAction", new { modelString = XYZ.Serialize(Model) })%>

控制器:获取 在 Action 方法中将对象作为字符串并在之前将其转换回对象

 using public ActionResult SomeAction(string modelString) { XYX xyz = XYX.Deserialize(modelString); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2017-02-22
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 2021-10-05
    相关资源
    最近更新 更多