码迷,mamicode.com
首页 > 其他好文 > 详细

啊啊啊

时间:2015-08-13 17:35:18      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

@{

    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=‘width:50px;‘>货位</td><td style=‘width:100px;‘>商品详情</td><td style=‘width:50px;‘></td><td style=‘width: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技术分享技术分享
 
    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. }  

啊啊啊

标签:

原文地址:http://www.cnblogs.com/niejun/p/4727492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!