在 ASP.NET MVC 中使用 SweetAlert 捕获错误
Make a catching error using SweetAlert in ASP.NET MVC
我的控制器在像这样上传时会执行 try catch :
try
{
connExcel.Open();
cmdExcel.CommandText = "SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
// dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
//here the Sweetalert or just alert
}
我想制作一个 sweetalert2 弹出窗口,在不满足 try 条件时提示“模板不匹配”。
我该怎么办?我在_layout里也安装了swal库,因为我之前在JS里用过Ajax,但是还是对这个case很迷惑
整个动作:
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
var path = Server.MapPath("~/Uploads/");
var filePath = string.Empty;
var extension = string.Empty;
var dtSheet = new DataTable();
var ExcelData = new DataSet();
var user = User.Identity.Name;
var prefix = user + DateTime.Now.ToString("yyyyMMddHHmmss");
if (postedFile != null)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
filePath = path + Path.GetFileName(postedFile.FileName.Replace(postedFile.FileName,prefix));
extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath+extension);
}
if (postedFile == null)
return new EmptyResult();
var connectionString = string.Empty;
//milih constring tergantung file
switch (extension)
{
case ".xls": //<= 2003 version
connectionString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //>= 2007 version
connectionString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
//oledb koneksi
connectionString = string.Format(connectionString, filePath+extension);
using (var connExcel = new OleDbConnection(connectionString))
{
using (var cmdExcel = new OleDbCommand())
{
using (var odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//ambil nama sheet #1
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
try
{
connExcel.Open();
cmdExcel.CommandText =
"SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
//dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
//here the Sweetalert or just alert
}
}
}
}
ExcelData.Tables.Add(dtSheet);
return View(ExcelData);
}
谢谢:)
您不能 return 来自控制器的 sweetalert。您必须将其设置为查看侧面。这是例子
try
{
connExcel.Open();
cmdExcel.CommandText = "SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
// dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
ViewBag.Message = "Your Message";
}
在脚本标记的 html 页面上。
<script>
$(function() {
var message = '@ViewBag.Message';
if (message != '')
swal({ title: "Done", text: message , icon: "error" });
});
</script>
我的控制器在像这样上传时会执行 try catch :
try
{
connExcel.Open();
cmdExcel.CommandText = "SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
// dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
//here the Sweetalert or just alert
}
我想制作一个 sweetalert2 弹出窗口,在不满足 try 条件时提示“模板不匹配”。
我该怎么办?我在_layout里也安装了swal库,因为我之前在JS里用过Ajax,但是还是对这个case很迷惑
整个动作:
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
var path = Server.MapPath("~/Uploads/");
var filePath = string.Empty;
var extension = string.Empty;
var dtSheet = new DataTable();
var ExcelData = new DataSet();
var user = User.Identity.Name;
var prefix = user + DateTime.Now.ToString("yyyyMMddHHmmss");
if (postedFile != null)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
filePath = path + Path.GetFileName(postedFile.FileName.Replace(postedFile.FileName,prefix));
extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath+extension);
}
if (postedFile == null)
return new EmptyResult();
var connectionString = string.Empty;
//milih constring tergantung file
switch (extension)
{
case ".xls": //<= 2003 version
connectionString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //>= 2007 version
connectionString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
//oledb koneksi
connectionString = string.Format(connectionString, filePath+extension);
using (var connExcel = new OleDbConnection(connectionString))
{
using (var cmdExcel = new OleDbCommand())
{
using (var odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//ambil nama sheet #1
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
try
{
connExcel.Open();
cmdExcel.CommandText =
"SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
//dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
//here the Sweetalert or just alert
}
}
}
}
ExcelData.Tables.Add(dtSheet);
return View(ExcelData);
}
谢谢:)
您不能 return 来自控制器的 sweetalert。您必须将其设置为查看侧面。这是例子
try
{
connExcel.Open();
cmdExcel.CommandText = "SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
// dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
ViewBag.Message = "Your Message";
}
在脚本标记的 html 页面上。
<script>
$(function() {
var message = '@ViewBag.Message';
if (message != '')
swal({ title: "Done", text: message , icon: "error" });
});
</script>