@{
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 += "姓名:" + "张三" + ' ' + "电话:" + "15888888888";
html += "<br />";
html += "地址:上海市浦东新区xxxxxxx";
html += "<br />";
html += "配送时间:2015-15-15 10:10:10" + ' ' + "店铺: 长寿路店";
html += "<br />";
html += "总价:¥42.00" + ' ' + "客户电话: 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
- public static T Json2Obj<T>(string json)
- {
- T obj = Activator.CreateInstance<T>();
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
- {
- System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
- return (T)serializer.ReadObject(ms);
- }
- }