📂 C# Common
Tới nhanh
Controller
TVP Report
Search List
Save List
TVP
New Rec
ToSqlRecord
JS Export
JS Template
Excel Upload
Sample Controller
using bujeon.Attributor;
using bujeon.Helper;
using bujeon.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace bujeon.Controllers
{
[ExActionFilter]
[ExAuthorize(Roles = "TCODE")]
public class TCODEController : BaseController
{
public ActionResult Index()
{
//-----------------------------------------------
// Set Screen Title and Program ID
//-----------------------------------------------
InitializePage("TCODE");
ViewBag.FROM = "";
//-----------------------------------------------
// Get grid data
//-----------------------------------------------
ComGridModel model = new ComGridModel();
model.LoadProgInfo("TCODE", "GRID_1");
ViewBag.Grid1_Models = model.GetColModels();
model.LoadProgInfo("TCODE", "GRID_1_H");
ViewBag.Grid1_Head_Models = model.GetColModels();
ViewBag.FACCO = model.GetFacco(false);
return View("TCODE", new BaseModel());
}
}
}
📋 Copy
✅ Copied!
TVP With Report
DataTable tvpTable1 = new DataTable("TVP_NAME");
tvpTable1.Columns.Add("ITEM", typeof(string));
foreach (var item in List)
{
DataRow row = tvpTable1.NewRow();
row["ITEM"] = item.ITEM;
tvpTable1.Rows.Add(row);
}
📋 Copy
✅ Copied!
Common Search List
public List SearchList()
{
List result;
using (SqlConnection conn = base.GetDB())
{
DynamicParameters parameters = new DynamicParameters();
//---------------------------------------
// Setup Params
//---------------------------------------
parameters.Add("LTPNM", this.Condition.LTPNM);
// General Params
parameters.Add("ERNAM", base.GetUserID());
parameters.Add("LANG", base.GetUserLang());
parameters.Add("DATTP", base.GetUserDATTP());
//---------------------------------------
// Output Params
//---------------------------------------
parameters.Add("RESULT", string.Empty, DbType.String, ParameterDirection.Output, 1);
parameters.Add("MSG", string.Empty, DbType.String, ParameterDirection.Output, 1000);
//---------------------------------------
// Execute procedure
//---------------------------------------
result = (List)conn.Query("SP_NAME", param: parameters, commandType: CommandType.StoredProcedure);
//GridReader reader = conn.QueryMultiple("UP_MES072540_SER_13_V2", param: parameters, commandType: CommandType.StoredProcedure);
//Factories = reader.Read().ToList();
//SearchRecs = reader.Read().ToList();
//---------------------------------------
// Set procedure output values
//---------------------------------------
base.Result = parameters.Get("RESULT");
base.Msg = parameters.Get("MSG");
}
return result;
}
📋 Copy
✅ Copied!
Common Save List
public void SaveList()
{
using (SqlConnection conn = base.GetDB())
using (SqlTransaction trans = conn.BeginTransaction())
{
try
{
DynamicParameters parameters = new DynamicParameters();
//---------------------------------------
// Setup Params
//---------------------------------------
parameters.Add("U_TABLE", CreateInsertTVP().AsTableValuedParameter());
// General Params
parameters.Add("ERNAM", base.GetUserID());
parameters.Add("LANG", base.GetUserLang());
parameters.Add("DATTP", base.GetUserDATTP());
//---------------------------------------
// Output Params
//---------------------------------------
parameters.Add("RESULT", string.Empty, DbType.String, ParameterDirection.Output, 1);
parameters.Add("MSG", string.Empty, DbType.String, ParameterDirection.Output, 1000);
//---------------------------------------
// Execute procedure
//---------------------------------------
conn.Execute("UP_MES072330_INS_01", param: parameters, transaction: trans, commandType: CommandType.StoredProcedure);
//---------------------------------------
// Set procedure output values
//---------------------------------------
base.Result = parameters.Get("RESULT");
base.Msg = parameters.Get("MSG");
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
}
📋 Copy
✅ Copied!
Common TVP
private List TVP(List ListItem)
{
List result = new List();
foreach (DOType item in ListItem)
{
result.Add(item.ToSqlRecord(base.LoginInfo));
}
return result;
}
📋 Copy
✅ Copied!
Common New Record
public static DOType ForNewRec()
{
return new DOType()
{
FIELD = ""
};
}
📋 Copy
✅ Copied!
Common To SQL Record
public SqlDataRecord ToSqlRecord(MES000000Model.DOLoginInfo loginInfo)
{
SqlDataRecord record = new SqlDataRecord(MetaTVP.TvpMeta_NAME);
record.SetSqlString(0, string.IsNullOrEmpty(this.FIELD) ? null : this.FIELD);
return record;
}
📋 Copy
✅ Copied!
[JS] Common Export Function
function onExportFile(type) {
const gridData = grid.getData();
const columns = grid.getColumns();
const filteredColumns = columns.map(c => ({
...c,
header: c.header?.replace(/<\s*br\s*\/?\s*>/gi, ' ')
}));
const tempContainer = document.createElement('div');
document.body.appendChild(tempContainer);
const tempGrid = new tui.Grid({
el: tempContainer,
data: gridData,
columns: filteredColumns,
});
tempGrid.export(type, {
fileName: '@ViewBag.ProgID',
});
tempGrid.destroy();
}
📋 Copy
✅ Copied!
[JS] Common Download Template Function
function onDownloadTemplate() {
var param = {
Condition: {
EMHNO: V_EMHNO
}
};
down_submit('@Url.Action("ExportFormat", "MES072641")', 'POST', param);
}
📋 Copy
✅ Copied!
[C#] Common Excel Upload
public void ExcelUpload()
{
GridModel.LoadProgInfo("MES....", "GRID_1");
if (GridModel.Result != Defines.ResultCode.SUCCESS)
{
this.Result = GridModel.Result;
this.Msg = GridModel.Msg;
}
try
{
List ListItem = new List();
using (Workbook Book = new Workbook())
{
ExcelUpUtils excel = new ExcelUpUtils();
if (excel.LoadWorkbookFromUpload(Book, this.ExcelUp) == false)
{
base.Result = ResultCode.ERROR;
base.Msg = RES_Messages.ExcelReadFail;
return;
}
Worksheet Sheet = Book.Worksheets[0];
int bottomRowIdx = Sheet.GetUsedRange().BottomRowIndex;
CellCollection Cells = Sheet.Cells;
int RowIdx = 4;
while (RowIdx <= bottomRowIdx)
{
DOExcelUpload5 rec = DOExcelUpload5.ForNewRec();
rec.LTPNM = Cells[RowIdx, 0].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 0].Value.ToString().Trim();
rec.MACNM = Cells[RowIdx, 1].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 1].Value.ToString().Trim();
rec.PART2 = Cells[RowIdx, 2].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 2].Value.ToString().Trim();
rec.ZTYPE = Cells[RowIdx, 3].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 3].Value.ToString().Trim();
rec.BRTNO_TX = Cells[RowIdx, 4].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 4].Value.ToString().Trim();
rec.PATNM = Cells[RowIdx, 5].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 5].Value.ToString().Trim();
rec.MAKTX = Cells[RowIdx, 6].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 6].Value.ToString().Trim();
rec.DWGNO = Cells[RowIdx, 7].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 7].Value.ToString().Trim();
rec.MFRCR_TX = Cells[RowIdx, 8].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 8].Value.ToString().Trim();
rec.PARTP_TX = Cells[RowIdx, 9].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 9].Value.ToString().Trim();
rec.IQTY1 = Cells[RowIdx, 10].Value.ToString();
rec.IQTY2 = Cells[RowIdx, 11].Value.ToString();
rec.IQTY3 = Cells[RowIdx, 12].Value.ToString();
rec.SPCOD = Cells[RowIdx, 13].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 13].Value.ToString().Trim();
rec.MATNR = Cells[RowIdx, 14].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 14].Value.ToString().Trim();
rec.USEYN = Cells[RowIdx, 15].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 15].Value.ToString().Trim();
rec.DESCR = Cells[RowIdx, 16].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 16].Value.ToString().Trim();
rec.LTMNO = Cells[RowIdx, 17].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 17].Value.ToString().Trim();
rec.LTMVR = Cells[RowIdx, 18].Value.ToString();
rec.LTMSQ = Cells[RowIdx, 19].Value.ToString();
rec.PLSEQ = Cells[RowIdx, 20].Value.ToString();
rec.MFRCR = Cells[RowIdx, 21].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 21].Value.ToString().Trim();
rec.PARTP = Cells[RowIdx, 22].Value.ToString().Trim() == "" ? null : Cells[RowIdx, 22].Value.ToString().Trim();
ListItem.Add(rec);
RowIdx++;
}
}
if (ListItem.Count == 0)
{
this.Result = Defines.ResultCode.ERROR;
this.Msg = RES_Messages.NoExcelList;
}
// Store Data
using (SqlConnection conn = base.GetDB())
{
DynamicParameters parameters = new DynamicParameters();
//---------------------------------------
// Input Params
//---------------------------------------
parameters.Add("U_TABLE_1", ExcelTVP5(ListItem).AsTableValuedParameter());
// Common Params
parameters.Add("ERNAM", base.GetUserID());
parameters.Add("LANG", base.GetUserLang());
parameters.Add("DATTP", base.GetUserDATTP());
//---------------------------------------
// Output Params
//---------------------------------------
parameters.Add("RESULT", string.Empty, DbType.String, ParameterDirection.Output, 1);
parameters.Add("MSG", string.Empty, DbType.String, ParameterDirection.Output, 1000);
//---------------------------------------
// Execute procedure
//---------------------------------------
ExcelRec5 = (List)conn.Query("UP_MES072300_SER_22", param: parameters, commandType: CommandType.StoredProcedure);
//---------------------------------------
// Set procedure output values
//---------------------------------------
base.Result = parameters.Get("RESULT");
base.Msg = parameters.Get("MSG");
}
}
catch (Exception ex)
{
logger.Error(ex.Message, ex);
this.Result = Defines.ResultCode.ERROR;
this.Msg = RES_Messages.ExcelReadFail;
}
}
📋 Copy
✅ Copied!