我有控制器在像這樣上傳時嘗試捕獲:
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 彈出視窗,顯示“模板不匹配”。
我該怎么辦?我也在 _layout 中安裝了 swal 庫,因為我之前在 JS Ajax 中使用過它,但仍然對這種情況感到困惑。
整個動作:
[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);
}
謝謝 :)
uj5u.com熱心網友回復:
您不能從控制器回傳 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/417990.html
標籤:
