首先还是先在Model层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YM.Model.CustomModel
{
public class PartHistory
{
public int ID { get; set; }
public string PartCode { get; set; }//总件号
public string TaskNum { get; set; }//任务号
public string SingleAcount { get; set; }//单台数量
public string Acount { get; set; }//加工数量
public string FinishDate { get; set; }
public string StorageDate { get; set; }
public string StorageAmount { get; set; }//要求日期
public string Beizhu { get; set; }//属于哪个总件
public string PartName { get; set; }//子件的零件名称
public string PartSize { get; set; }//零件规格
public string FPartUnit { get; set; }//单位
}
}
然后BLL层
namespace YM.BLL.Tables
{
public class PartHistoryBll
{
public DataTable GetPartHistory(string PartCode)
{
string sql = "select * from t_PartHistory1 where PartCode like @PartCode";
return SqlHelper.GetTable(sql, CommandType.Text, new SqlParameter("@PartCode", PartCode + "%"));
}
最关键的地方views里:
在开头要添加jquery包,相信大家都会,不多做赘述。
$(function () {
$("#btnSelectData").click(function () { //#btnSelectData按钮的ID,.click点击事件
alert("查询成功");//弹出文本框
$.ajax({
url: '/Production/PartsHistoryInformationQuery111',//url 链接到控制器下的方法
type: 'post',//请求方式为post
data: { "PartCode": $("#txtPartCode").val() },//获取文本框的值
success: function (context) {//回调函数
if (context.Result == "OK") {
//循环遍历
var partCode = context.PartCode1;
$("#txtPartCode").val(partCode);
var data = eval(unescape(context.Msg));
var htmlTr = "";
//此处为拼表单 通过表单的ID 将值显示到页面上
for (var i = 0; i < data.length; i++) {
htmlTr += '<tr class="TableTr"><td class="A" >' + i + '</td></td><td><input type="text" name="" id="StockCount" value="' + data[i].TaskNum + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].PartCode + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].PartName + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].PartUnit + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].SingleAmount + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].Amount + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].FinishDate + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].StorageDate + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].StorageAmount + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].Customer + '" placeholder="" readonly="readonly" style="text-align:center"><td><input type="text" name="" id="StockCount" value="' + data[i].Remark + '" placeholder="" readonly="readonly" style="text-align:center">';
}
$("#htmlTable").html(htmlTr);
}
else {
layer.alert("提示:" + context.Msg, { icon: 0 });
return false;
}
}
});
});
});
<select class="form-control" name="search" id="search">
<!-- <option value="renwuhao">任务号</option>-->
<option value="lingjiandaima">零件代码</option>
<!-- <option value="lingjianmingcheng">零件名称</option>-->
</select>
<input class="form-control" type="text" id="txtPartCode" value="" placeholder="请填写查询数据">
<button class="btn btn-default" type="button" id="btnSelectData">查询</button>
</div><!-- 操作按钮end -->
<tr class="TableTr">
<td>序号</td>
<td>任务号</td>
<td>零件代码</td>
<td>零件名称</td>
<td>单位</td>
<td>单台数量</td>
<td>数量</td>
<td>完成日期</td>
<td>入库日期</td>
<td>入库数量</td>
<td>客户</td>
<td>备注</td>
</tr>
<tbody id="htmlTable">
<tr class="TableTr">
<td>1</td>
<td><input type="text" name="" id="TaskNum" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="PartCode" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="PartName" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="PartUnit" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="SingleAmount" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="Amount" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="FinishDate" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="StorageDate" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="Customer" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
<td><input type="text" name="" id="Remark" value="" placeholder="" readonly="readonly" style="text-align:center"></td>
</tr>
</tbody>
//readonly="readonly 查询结果为只读
控制器里的方法:
//零件加工历史信息查询
public JsonResult PartsHistoryInformationQuery111(string PartCode)
{
userInfo1 = (UserInfo)Session["UserInfo"];
ViewBag.Name = userInfo1.RealName;
PartHistoryBll partHistoryBll = new PartHistoryBll();
//AAA,BBB,CCC
DataTable dt = partHistoryBll.GetPartHistory(PartCode);//查询数据库
DataSet ds = new DataSet();
ds.Tables.Add(dt);
var jsonList = ObjectConvert.DataSetToJson(ds, "ds");
jsonList = Microsoft.JScript.GlobalObject.escape(jsonList);
return Json(new { Result = "OK", PartCode1 = PartCode, Msg = jsonList });//返回Json数组
}
最后在COMMON层封装一个公共类: 将Json转换成对象 对象转换成Json
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace YM.Common.Common
{
public class ObjectConvert
{
/// <summary>
/// Json转换成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonText"></param>
/// <returns></returns>
public static T JsonToObject<T>(string jsonText)
{
DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
T obj = (T)s.ReadObject(ms);
ms.Dispose();
return obj;
}
/// <summary>
/// 对象转换成JSON
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjectToJSON<T>(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
string result = string.Empty;
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
ms.Position = 0;
using (StreamReader read = new StreamReader(ms))
{
result = read.ReadToEnd();
}
}
return result;
}
public static string DataSetToJson(DataSet ds, string tblname)
{
string json = string.Empty;
try
{
if (ds.Tables.Count == 0)
throw new Exception("查询结果为空");
//json = "{";
for (int i = 0; i < ds.Tables.Count; i++)
{
//json += "\"" + tblname + "\"" + ":[";
json += "[";
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
json += "{";
for (int k = 0; k < ds.Tables[i].Columns.Count; k++)
{
json += "\"" + ds.Tables[i].Columns[k].ColumnName + "\"" + ":\"" + ds.Tables[i].Rows[j][k].ToString().Replace("\r\n", "").Replace("\n", "").Replace("\r", "").Replace("\"", "'") + "\"";
if (k != ds.Tables[i].Columns.Count - 1)
json += ",";
}
json += "}";
if (j != ds.Tables[i].Rows.Count - 1)
json += ",";
}
json += "]";
if (i != ds.Tables.Count - 1)
json += ",";
}
//json += "}";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return json;
}
/// <summary>
/// List<T> 转换成DataSet
/// </summary>
/// <typeparam name="T">对象</typeparam>
/// <param name="list">集合</param>
/// <returns>DataSet</returns>
public static DataSet ConvertToDataSet<T>(List<T> list)
{
if (list == null || list.Count <= 0)
{
return null;
}
DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
}
}