• 啊啊啊


    @{

        Layout = null;

    }

    <!DOCTYPE html>

    <html>

    <head>

       <script src="~/Scripts/jquery-1.8.2.min.js"></script>

       <meta name="viewport" content="width=device-width" />

       <title>Index</title>

       <script type="text/javascript">

           function preview() {

                $.ajax({

                    type: "POST",

                    url: "/Printpage/PrintPages",

                    datatype: "json",

                    success: function (data) {

                       var html = "";

                       var valdata = eval(data);

                        html += "-------------------------------------------";

                        html += "<br />";

                        html += "订单编号:" + valdata[0].OrderNo;

                        html += "<br />";

                        html += "-------------------------------------------";

                        html += "<br />";

                        html += "姓名:" + "张三" + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + "电话:" + "15888888888";

                        html += "<br />";

                        html += "地址:上海市浦东新区xxxxxxx";

                        html += "<br />";

                        html += "配送时间:2015-15-15 10:10:10" + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + "店铺: 长寿路店";

                        html += "<br />";

                        html += "总价:¥42.00" + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + "客户电话: 1588888888";

                        html += "<br />";

                        html += "-------------------------------------------";

                        html += "<br />";

                       var table = "<table><tr><td style='50px;'>货位</td><td style='100px;'>商品详情</td><td style='50px;'></td><td style='50px;'></td></tr>";

                 

                        $.each(valdata[0].good, function (i, v) {

                            table += "<tr>";

                            table += "<td>" + v.Number + "</td>";

                            table += "<td>" + v.Name + "</td>";

                            table += "<td>" + v.BuyCount + "</td>";

                            table += "<td>" + v.Price + "</td>";

                            table += "</tr>";

                       });

                        table += "</table>";

                        html += table;

                        window.document.body.innerHTML = html;

                        window.print();

                   },

                    error: function () {

                        alert("error");

                   }

               });

           }

       </script>

    </head>

    <body>

       <div>

           <a onclick="preview()" href="#"> 打印</a>

       </div>

    </body>

    </html>

    using MvcApplication2.Models;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;

    namespace MvcApplication2.Controllers
    {
    public class PrintPageController : Controller
    {
    //
    // GET: /PrintPage/

    public ActionResult Index()
    {
    return View();
    }
    [HttpPost]
    public string PrintPages()
    {
    List<Good> listGoods = new List<Good>()
    {
    new Good{ Name ="苹果", BuyCount=1, Number = 1, Price=12.0},
    new Good{ Name ="香蕉", BuyCount=2, Number = 2, Price=13.0},
    new Good{ Name ="荔枝", BuyCount=3, Number = 3,Price=14.0},
    new Good{ Name ="核桃", BuyCount=4, Number = 4, Price=15.0}
    };

    List<Order> listOrders = new List<Order>()
    {
    new Order{OrderNo = "123", good = listGoods}
    };
    return ToJsJson(listOrders);
    }

    public static string ToJsJson(object item)
    {
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
    using (MemoryStream ms = new MemoryStream())
    {
    serializer.WriteObject(ms, item);
    StringBuilder sb = new StringBuilder();
    sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
    return sb.ToString();
    }
    }

    }
    }

    单个对象转JSON

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
      1. public static T Json2Obj<T>(string json)   
      2. {  
      3.     T obj = Activator.CreateInstance<T>();  
      4.     using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))  
      5.     {  
      6.         System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());  
      7.         return (T)serializer.ReadObject(ms);  
      8.     }  
      9. }  
  • 相关阅读:
    利用matplotlib进行数据可视化
    《操作系统》课程笔记(Ch11-文件系统实现)
    《操作系统》课程笔记(Ch10-文件系统)
    《数据库原理》课程笔记 (Ch06-查询处理和优化)
    《计算机网络》课程笔记 (Ch05-网络层:控制平面)
    《计算机网络》课程笔记 (Ch04-网络层:数据平面)
    《计算机网络》课程笔记 (Ch03-运输层)
    东南大学《软件测试》课程复习笔记
    《数据库原理》课程笔记 (Ch05-数据库存储结构)
    《操作系统》课程笔记(Ch09-虚拟内存)
  • 原文地址:https://www.cnblogs.com/niejun/p/4727492.html
Copyright © 2020-2023  润新知